我需要计算方法完成所需的总时间,通常我使用:
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();
那么我将如何找到线程方法完成的总时间呢?