Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在ExecutorService中等待可运行任务完成的有效方法_Java_Multithreading_Executorservice - Fatal编程技术网

Java 在ExecutorService中等待可运行任务完成的有效方法

Java 在ExecutorService中等待可运行任务完成的有效方法,java,multithreading,executorservice,Java,Multithreading,Executorservice,我有n个使用ExecutorService执行的Runnable任务(不可调用) 我想等待所有任务完成 我不能使用invokeAll——因为它适用于可调用的集合 我无法使用shutdown()+awaitTermination,因为awaitTermination需要提供超时,但我的任务可能需要数小时才能完成 我可以使用: ExecutorService.shutdown(); while (!ExecutorService.isTerminated()) {} 但这

我有n个使用ExecutorService执行的
Runnable
任务(不可调用)

我想等待所有任务完成

我不能使用
invokeAll
——因为它适用于可调用的集合

我无法使用
shutdown()+awaitTermination
,因为awaitTermination需要提供超时,但我的任务可能需要数小时才能完成

我可以使用:

ExecutorService.shutdown();             
while (!ExecutorService.isTerminated()) {}
但这个循环总是会被触发的


在这种情况下有什么建议?

您可以使用
ExecutorService.awaitTermination(Long.MAX\u值,TimeUnit.HOURS)

您可以使用执行器服务.awaitTermination(Long.MAX_值,TimeUnit.HOURS)

ExecutorService.awaitTermination()
返回一个
布尔值,指示执行器是否已终止或超时已过。当然,您可以在循环中调用它:

ExecutorService executor = ...;

executor.shutdown();
while (!executor.awaitTermination(24L, TimeUnit.HOURS)) {
    System.out.println("Still waiting for the executor to finish");
}

System.out.println("Executor finished");

ExecutorService.awaitTermination()
返回一个
布尔值
,指示执行器是否已终止或超时已过。当然,您可以在循环中调用它:

ExecutorService executor = ...;

executor.shutdown();
while (!executor.awaitTermination(24L, TimeUnit.HOURS)) {
    System.out.println("Still waiting for the executor to finish");
}

System.out.println("Executor finished");

对于已知数量的任务来说,这是完美的,但也有一些情况,当你不知道你将有多少任务时,在这种情况下,我使用。例如:

    Semaphore s =new Semaphore(0);
    while(..){
     if (isLastTask){
         taskExecutor.execute(new Task(s));
     } else 
         taskExecutor.execute(new Task());
    }
    s.acquire(1);

class Task implement implements Runnable {
   Semaphore s;

   public Task(){
     this(null);
   }

   public Task (Semaphore s){
     this.s = s;
   }

   public void run(){
       ......
      if ( s != null )
          s.release();
   }
}

对于已知数量的任务来说,这是完美的,但也有一些情况,当你不知道你将有多少任务时,在这种情况下,我使用。例如:

    Semaphore s =new Semaphore(0);
    while(..){
     if (isLastTask){
         taskExecutor.execute(new Task(s));
     } else 
         taskExecutor.execute(new Task());
    }
    s.acquire(1);

class Task implement implements Runnable {
   Semaphore s;

   public Task(){
     this(null);
   }

   public Task (Semaphore s){
     this.s = s;
   }

   public void run(){
       ......
      if ( s != null )
          s.release();
   }
}

在我的帖子中,我特别写了为什么我不能使用它。在我的Java TimeUnit版本中没有小时或日秒,Long.MAX_VALUE of SECONDS=106751991167300 day在我的帖子中,我特别写了为什么我不能使用它。在我的Java TimeUnit版本中没有小时或日秒,Long.MAX_VALUE of seconds=106751991167300 days必须在类声明之前关闭while循环。while循环必须在类声明之前关闭。