2014-11-22 37 views
-1

我想有3種方法在同一時間,所以我可以運行例如連續添加3次,或者減去並隨時檢查。我該怎麼做呢?到目前爲止,我的輸出僅僅是三種方法。線程在Java中,其中3種名爲方法同時運行

import java.util.Scanner; 

public class Calc { 

    public static void main(String[] args) { 

     Thread t1 = new Thread(new Runnable() { 

      @Override 
      public void run() { 
       while (true) { 
        add(); 
        remove(); 
        check(); 
        try { 
         Thread.sleep(20); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 
     }); 
     t1.start();  

    } 

    static int total = 0; 

    static Scanner s = new Scanner(System.in); 

    static synchronized int add() { 
     System.out.println("How much was added?"); 
     int a = s.nextInt(); 
     total = total + a; 
     return total; 
    } 

    static synchronized int remove() { 
     System.out.println("How much was removed?"); 
     int b = s.nextInt(); 
     return total = total - b; 
    } 

    static synchronized void check() { 
     System.out.println("Would you like to know how much is left?"); 
     String str = s.next(); 
     if (str.equals("Yes")) 
      System.out.println(total); 

    } 

} 
+2

你只需要一個線程或期望越來越高,你肯定要靜態方法的? – SMA 2014-11-22 14:35:26

+0

「同時運行」並不意味着你認爲這意味着什麼...你的實際目標是什麼? – specializt 2014-11-22 16:20:36

回答

0

我想你混淆各種各樣的 - 你不需要在所有的多個線程,一個簡單的命令界面一切必要措施,使您的工作分配像你想它的行爲:

import java.io.IOException; 
import java.util.Arrays; 
import java.util.Scanner;  


public class Calc { 

    private final Scanner scanner = new Scanner(System.in); 
    private static Calc instance; 
    private int current = 0; 

    Calc() 
    {  
     mainThread.start(); 
    } 

    private final Thread mainThread = new Thread() 
    { 
     @Override 
     public void run() { 
      mainLoop();   
     } 

    }; 

    private void mainLoop() 
    { 
     boolean end = false; 
     while(!end) 
     { 
      System.out.println("what would you like to do? [ \"A\" = ADD, \"R\" = REMOVE, \"C\" = CHECK, \"Q\" = QUIT ]"); 
      String command = scanner.next(); 
      switch(command.trim().toUpperCase()) 
      { 
      case "A" : add(); break; 
      case "R" : subtract(); break; 
      case "C" : check(); break; 
      case "Q" : 
      { 
       System.out.println("exiting, last value was " + current); 
       end = true; 
       break; 
      } 
      default : System.err.println("unknown command!"); break; 
      } 
     }  
    } 

    private void add() 
    { 
     System.out.println("how much would you like to add?"); 
     int val = scanner.nextInt(); 
     current += val; 
    } 

    private void subtract() 
    { 
     System.out.println("how much would you like to subtract?"); 
     int val = scanner.nextInt(); 
     current -= val; 
    } 

    private void check() 
    { 
     System.out.println("current value : " + current); 
    } 


    public static void main(String[] args) throws IOException { 
     instance = new Calc();    
    } 

} 

所有這些都是有效的,因爲只有一個實例,如果有多個實例會遇到很多問題 - 這些被稱爲「競賽條件」。

我的建議是:如果不要使用線程你不是一個熟練的專家,即使是最簡單的線程(併發訪問域)需要某種形式的同步對象。

如果你真的開始學習有關線程始終使用Semaphore S,CountdownLatch ES或類似的東西 - 並讓他們私有靜態的,當然。

這似乎是一個很好的教程:http://www.tutorialspoint.com/java/java_multithreading.htm

相關問題