我试图在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()来阻止并等待结果。