2012-11-13 170 views
0

時間,我需要花費的時間的方法來完成的總時間,我通常使用:線程池的Java

long startTime = System.currentTimeMillis(); 
MethodWithThreads(); // the method which uses threading 
long endTime = System.currentTimeMillis(); 
total = endTime - startTime; 

現在obiviously的問題是,mainthread線程的其餘部分之前終止。在線程函數內部是下面的代碼:

int cores = Runtime.getRuntime().availableProcessors(); 
ExecutorService pool = Executors.newFixedThreadPool(cores-1); 
for(int i = 0; i < 1000; i++) 
    { 
     pool.submit(new RunnableThread()); 
    } 
pool.shutdown(); 

那麼我如何找到線程方法完成的總時間?

回答

1

愚蠢的我......應該做更多的研究。 我在while循環中使用isTerminated並等待終止。

while(!pool.isTerminated()) 
      { 
       try { 
       pool.awaitTermination(1000, TimeUnit.SECONDS); 
       } catch (InterruptedException ex) { 
        Logger.getLogger(ThreadManagement.class.getName()).log(Level.SEVERE,null, ex); 
       } 
      } 
      long endTime = System.currentTimeMillis();