简体   繁体   中英

timing threadpool java

I need to time the total time it takes for a method to complete, usually i use:

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

Now obiviously the problem is that the mainthread terminates before the rest of the threads. Inside the thread function is the following code:

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

So how am i going to find the total time for the thread method to finish?

Stupid me... should have done more research. I used the isTerminated in a while loop and awaited termination.

 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM