ExecutorService FixedThreadPool 作为共享参数不是运行并行
ExecutorService FixedThreadPool as a shared parameter not running in parallel
我正在尝试在 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 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() 来阻塞并等待结果。