我需要确定一个方法完成的总时间,通常我使用:

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


现在显然问题是主线程在其余线程之前终止。线程函数内部是以下代码:

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


那么,我将如何查找线程方法完成的总时间?

最佳答案

愚蠢的我...应该做更多的研究。
我在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();

09-05 11:34