2011-01-22 60 views
-2

我在Java中使用Thread。我創建擴展Thread類和它的罰款,但問題是,當調用此Thread,我不知道這個類的實例的數量,因爲用戶必須輸入這個號碼。例如:如何在Java中使用線程?

multiThread multiThreadInstance = new multiThread(/* number entered from user */); 
multiThreadInstance.start(); 

這將調用這個線程一次,但如果我寫:

multiThread multiThreadInstance1 = new multiThread(/* number entered from user */) 
multiThreadInstance1.start() 
multiThread multiThreadInstance2 = new multiThread(/* number entered from user */) 
multiThreadInstance2.start() 

這將調用兩次在同一時間,等等。

如果我把它放在一個for循環,然後如果用戶輸入3,例如,然後start1運行時,當start1飾面,start2運行時,當start2飾面,start3運行。我需要讓這些線程的實例同時運行。我怎樣才能做到這一點?

+0

很抱歉,如果我錯過了一些東西,但我不知道太明白你的問題。首先是從用戶輸入的*號碼*在您的問題中很重要?你的問題中的所有例子似乎都在起作用。你可以發佈你的循環實現的代碼,並告訴我們什麼不工作或你不知道該怎麼做。 – gabuzo 2011-01-22 08:53:29

回答

1

您需要使用Java的高水平併發工具來做到這一點。看看倒數計時器和執行者。以下是可以做你想要的代碼。我建議你閱讀java併發實用程序。

import java.util.concurrent.*; 

public class ConcurrentTimer { 
    private ConcurrentTimer() { } // Noninstantiable 

    public static long time(Executor executor, int concurrency, 
      final Runnable action) throws InterruptedException { 
     final CountDownLatch ready = new CountDownLatch(concurrency); 
     final CountDownLatch start = new CountDownLatch(1); 
     final CountDownLatch done = new CountDownLatch(concurrency); 

     for (int i = 0; i < concurrency; i++) { 
      executor.execute(new Runnable() { 
       public void run() { 
        ready.countDown(); // Tell timer we're ready 
        try { 
         start.await(); // Wait till peers are ready 
         action.run(); 
        } catch (InterruptedException e) { 
         Thread.currentThread().interrupt(); 
        } finally { 
         done.countDown(); // Tell timer we're done 
        } 
       } 
      }); 
     } 

     ready.await();  // Wait for all workers to be ready 
     long startNanos = System.nanoTime(); 
     start.countDown(); // And they're off! 
     done.await();  // Wait for all workers to finish 
     return System.nanoTime() - startNanos; 
    } 
} 

上面提供的代碼示例的可運行版本:(編輯)

import java.util.concurrent.*; 

public class ConcurrentTimer { 

    public static void main(String[] args) { 


     try { 
      Runnable action = new Runnable() { 
        public void run() { 

         System.out.println("Thread Running"); 

        } 
       }; 

      time (3, action); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 


    private ConcurrentTimer() { } // Noninstantiable 

    public static long time(int concurrency, 
      final Runnable action) throws InterruptedException { 

     Executor executor = Executors.newFixedThreadPool(concurrency); 


     final CountDownLatch ready = new CountDownLatch(concurrency); 
     final CountDownLatch start = new CountDownLatch(1); 
     final CountDownLatch done = new CountDownLatch(concurrency); 

     for (int i = 0; i < concurrency; i++) { 
      executor.execute(new Runnable() { 
       public void run() { 
        ready.countDown(); // Tell timer we're ready 
        try { 
         start.await(); // Wait till peers are ready 
         action.run(); 
        } catch (InterruptedException e) { 
         Thread.currentThread().interrupt(); 
        } finally { 
         done.countDown(); // Tell timer we're done 
        } 
       } 
      }); 
     } 

     ready.await();  // Wait for all workers to be ready 
     long startNanos = System.nanoTime(); 
     start.countDown(); // And they're off! 
     done.await();  // Wait for all workers to finish 
     return System.nanoTime() - startNanos; 
    } 
} 
2

我懷疑您意外覆蓋了start()方法Thread。請確保它是run()方法你覆蓋。

如果您用自己的實現覆蓋了start()方法,那麼您將「移除Thread類的魔法」。奇妙之處在於start()它開始在一個新的線程run()方法的執行,所以保持你自己的代碼中run()

+0

我把run方法我自己的代碼,當我說multiThreadInstance.start()方法run工作 – 2011-01-22 07:49:20

+0

你有你的run方法的任何同步?例如`wait` /`notify`。 – aioobe 2011-01-22 07:52:59