我正在尝试在 Java 8 中使用固定线程池,只要它保持在同一函数内,它就可以完美工作。一旦我尝试将执行器共享为参数,它就永远不会并行运行。
这很好用:
```
public static void test2() {
ExecutorService executor = Executors.newFixedThreadPool(2);
try {
CompletionService<Integer> myCompletionService =
new ExecutorCompletionService<Integer>(executor);
myCompletionService.submit(()-> {
try {
TimeUnit.SECONDS.sleep(5);
return 123;
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
});
CompletionService<Integer> myCompletionService2 =
new ExecutorCompletionService<Integer>(executor);
myCompletionService2.submit(()-> {
try {
TimeUnit.SECONDS.sleep(5);
return 654;
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
});
Future<Integer> myFuture = myCompletionService.take();
Integer x = myFuture.get();
System.out.println("Result = " + x);
Future<Integer> myFuture2 = myCompletionService2.take();
Integer y = myFuture2.get();
System.out.println("Result = " + y);
executor.shutdown();
} catch (InterruptedException | ExecutionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
```
但是一旦我将它们移动到三个函数中,例如:
```
static Integer t1(ExecutorService executor) throws InterruptedException, ExecutionException {
CompletionService<Integer> myCompletionService =
new ExecutorCompletionService<Integer>(executor);
myCompletionService.submit(()-> {
try {
TimeUnit.SECONDS.sleep(5);
return 123;
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
});
Future<Integer> myFuture = myCompletionService.take();
return myFuture.get();
}
static Integer t2(ExecutorService executor) throws InterruptedException, ExecutionException {
CompletionService<Integer> myCompletionService2 =
new ExecutorCompletionService<Integer>(executor);
myCompletionService2.submit(()-> {
try {
TimeUnit.SECONDS.sleep(5);
return 456;
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
});
Future<Integer> myFuture2 = myCompletionService2.take();
return myFuture2.get();
}
static void test3() {
ExecutorService executor = Executors.newFixedThreadPool(5);
try {
Integer x = t1(executor);
Integer y = t2(executor);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
executor.shutdown();
}
``` 现在 test3 将需要 10 秒,我预计它与顶部的测试相同,如果并行运行,则应该需要 5 秒。,
最佳答案
在提交后的 t1 中,您调用 get() 并被阻止,因此仅当第一个任务完成时(5 秒后),您才从 t1 退出。
在第一个示例中,您提交了两个任务,以便它们开始在单独的线程中执行,然后仅调用 get() 来阻止并等待结果。
关于java - ExecutorService FixThreadPool 作为共享参数不并行运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53402069/