计时线程池java

计时线程池java,java,threadpool,benchmarking,timing,Java,Threadpool,Benchmarking,Timing,我需要计算方法完成所需的总时间,通常我使用: long startTime = System.currentTimeMillis(); MethodWithThreads(); // the method which uses threading long endTime = System.currentTimeMillis(); total = endTime - startTime; 现在明显的问题是主线程在其余线程之前终止。thread函数中包含以下代码: int cores = Run

我需要计算方法完成所需的总时间,通常我使用:

long startTime = System.currentTimeMillis();
MethodWithThreads(); // the method which uses threading
long endTime = System.currentTimeMillis();
total = endTime - startTime;
现在明显的问题是主线程在其余线程之前终止。thread函数中包含以下代码:

int cores = Runtime.getRuntime().availableProcessors();
ExecutorService pool = Executors.newFixedThreadPool(cores-1);
for(int i = 0; i < 1000; i++)
     {
      pool.submit(new RunnableThread());
     }
pool.shutdown();
int cores=Runtime.getRuntime().availableProcessors();
ExecutorService池=Executors.newFixedThreadPool(cores-1);
对于(int i=0;i<1000;i++)
{
submit(新的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();