简体   繁体   English

如何检查执行程序服务中的所有线程是否已完成

[英]how to check if all threads in executor service is done

I'm trying to calculate some metrics, once all the threads in the myclass are done, the job is done for the thread once i reach the timeout scenario, which i able to hit for every thread. 我正在尝试计算一些指标,一旦myclass中的所有线程都完成了,一旦达到超时情况,就可以为该线程完成工作,我可以为每个线程命中。 Now in the main () method in the Group class how can i wait for all the myclass threads to complete ? 现在,在Group类的main()方法中,如何等待所有myclass线程完成?

I dont want to use any sleep() scenario 我不想使用任何sleep()场景

Group Class

  class myGroup {

  public void run(int numThreads) {
      executor = Executors.newFixedThreadPool(numThreads);
      executor.submit(new myclass(threadNumber));

  }

  public static void main(String[] args) {

       run(noOfthreads);

       // Collect metrics once all the myclass threads are done.
   }

 }

myclass 我的课

  class myclass implements Runnable {

     try{
     }
     catch(SomeTimeoutException cte){

      // Job Done for thread
     }


  }

Could do something like this: 可以做这样的事情:

List<myclass> tasks = makeAListOfMyClassTasks();
// this will kick off all your tasks at once:
List<Future<myclass>> futures = executor.invokeAll(tasks);
// this will wait until all your tasks are done, dead, or the specified time has passed
executor.awaitTermination(10, TimeUnit.MINUTES); // change this to your liking

// check each Future to see what the status of a specific task is and do something with it
for (Future<myclass> future : futures) {
    if (future.isCancelled()) {
        // do something
    } else if (future.isDone()) {
        myclass task = future.get(); // this will block if it's not done yet
        task.whatever();
    }
}

@beatngu13 also pointed out this nifty class ExecutorCompletionService; @ beatngu13还指出了这个漂亮的类ExecutorCompletionService; so you could do something like this: 因此您可以执行以下操作:

List<myclass> tasks = makeAListOfMyClassTasks();
CompletionService<Result> ecs = new ExecutorCompletionService<Result>(exectuor);
for (myclass t : tasks) {
    // this kicks off the task and returns a Future, which you could gather
    ecs.submit(t);
}
for (int i = 0; i < tasks.length(); i ++) {
    myclass task = ecs.take().get(); // this will block until the next task is done/dead
    // ... do something with task
}

Info on futures: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html 期货信息: https : //docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html

This has examples for ExecutorService: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html 它具有ExecutorService的示例: https : //docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html

This related thread is pretty relevant: ExecutorService, how to wait for all tasks to finish 这个相关的线程非常相关: ExecutorService,如何等待所有任务完成

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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