Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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

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执行器可以';在执行过程中不添加新任务_Java_Multithreading - Fatal编程技术网

Java执行器可以';在执行过程中不添加新任务

Java执行器可以';在执行过程中不添加新任务,java,multithreading,Java,Multithreading,尝试使用executor并行运行某些任务。我的任务的特殊之处在于,其中一个执行器线程可能会在执行期间添加新任务(如下面的代码段所示) 下面的代码片段不能满足我的需要。如果有人能给我一些建议,我将不胜感激 public void testThread2() throws Exception { Table obj = new Table();//only one object ExecutorService taskExecutor = Executors.newFixe

尝试使用executor并行运行某些任务。我的任务的特殊之处在于,其中一个执行器线程可能会在执行期间添加新任务(如下面的代码段所示)

下面的代码片段不能满足我的需要。如果有人能给我一些建议,我将不胜感激

 public void testThread2() throws Exception {
      Table obj = new Table();//only one object
      ExecutorService taskExecutor = Executors.newFixedThreadPool(3);
      Random rand = new Random();
      final ArrayList<Integer> tasks = new ArrayList<>();
      tasks.add(1);
      tasks.add(2);
      tasks.add(3);
      tasks.add(4);
      for (Integer t: tasks) {
         taskExecutor.execute(() -> {
            for (int j = 0; j< 5; j++) {
               System.out.println(j * 100 + t);
               try {
                  Thread.sleep(rand.nextInt(300));
               } catch (Exception e) {
                  e.printStackTrace();
               }
            }
            if (t == 2) {
               tasks.add(5);
            }
         });
      }
      Thread.sleep(2000);
      taskExecutor.shutdown();
   }
public void testThread2()引发异常{
Table obj=new Table();//只有一个对象
ExecutorService taskExecutor=Executors.newFixedThreadPool(3);
Random rand=新的Random();
最终ArrayList任务=新建ArrayList();
增加(1);
任务。添加(2);
任务。添加(3);
增加(4);
for(整数t:任务){
taskExecutor.execute(()->{
对于(int j=0;j<5;j++){
系统输出打印项次(j*100+t);
试一试{
线程睡眠(兰特nextInt(300));
}捕获(例外e){
e、 printStackTrace();
}
}
如果(t==2){
增加(5);
}
});
}
《睡眠》(2000年);
taskExecutor.shutdown();
}

提取应该以额外方法运行的代码,并替换
任务。添加(5)taskExecutor.execute()
,执行代码>

public void testThread2()引发异常{
Table obj=new Table();//只有一个对象
ExecutorService taskExecutor=Executors.newFixedThreadPool(3);
Random rand=新的Random();
最终ArrayList任务=新建ArrayList();
增加(1);
任务。添加(2);
任务。添加(3);
增加(4);
for(整数t:任务){
taskExecutor.execute(()->testThread2Runner(t,taskExecutor));
}
《睡眠》(2000年);
taskExecutor.shutdown();
}
公共静态void testThread2Runner(int t,ExecutorService taskExecutor){
对于(int j=0;j<5;j++){
系统输出打印项次(j*100+t);
试一试{
线程睡眠(兰特nextInt(300));
}捕获(例外e){
e、 printStackTrace();
}
}
如果(t==2){
taskExecutor.execute(()->testThread2Runner(5,taskExecutor));
}
}

2次运行时,
for
循环可能已完成,或者(如果循环运行时间超过300ms),
for
循环将抛出一个
ConcurrentModificationException
@JohannesKuhn,您是对的。在我的例子中,我可以让主线程等待更长的时间。我最头疼的是,无法将任务添加到执行器线程中。谢谢@JohannesKuhn!你应该考虑使用<代码> FokCuangPoos<代码>,它们在任务内添加新任务时(通常称为<代码> Frk())/>代码>通常会有更好的性能。
 public void testThread2() throws Exception {
    Table obj = new Table();//only one object
    ExecutorService taskExecutor = Executors.newFixedThreadPool(3);
    Random rand = new Random();
    final ArrayList<Integer> tasks = new ArrayList<>();
    tasks.add(1);
    tasks.add(2);
    tasks.add(3);
    tasks.add(4);
    for (Integer t: tasks) {
        taskExecutor.execute(() -> testThread2Runner(t, taskExecutor));
    }
    Thread.sleep(2000);
    taskExecutor.shutdown();
}

public static void testThread2Runner(int t, ExecutorService taskExecutor) {
    for (int j = 0; j< 5; j++) {
        System.out.println(j * 100 + t);
        try {
            Thread.sleep(rand.nextInt(300));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (t == 2) {
        taskExecutor.execute(() -> testThread2Runner(5, taskExecutor));
    }
}