スレッド実行結果の取得
3313 ワード
スレッド実行中に例外が投げ出されると、結果の取得時にhangがエラースレッドにあり、他のスレッドの結果は取得されません.package com.test;
import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future;
public class ExecutorExceptionTest {
}
task0 begin********************* task1 begin********************* task0 end————————- task2 begin********************* task2 end————————- task3 begin********************* task4 begin********************* task4 end————————- Result [name=task0, value=number value is 0] Exception in thread “main” java.util.concurrent.ExecutionException: java.lang.Exception: task1the task failed! at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:188) at com.test.ExecutorExceptionTest.main(ExecutorExceptionTest.java:80) Caused by: java.lang.Exception: task1the task failed! at com.test.ExecutorExceptionTest Task.call(ExecutorExceptionTest.java:62)atcom.test.ExecutorExceptionTest Task.call(ExecutorExceptionTest.java:1) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)
import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future;
public class ExecutorExceptionTest {
static class Result {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Result(String name, String value) {
this.name = name;
this.value = value;
}
@Override
public String toString() {
return "Result [name=" + name + ", value=" + value + "]";
}
}
static class Task implements Callable {
private String name;
private int value;
public Task(String name, int value) {
this.name = name;
this.value = value;
}
@Override
public Result call() throws Exception {
System.out.println(name + " begin*********************");
if (value % 2 == 1)
throw new Exception(name + "the task failed!");
System.out.println(name + " end-------------------------");
return new Result(name, "number value is " + value);
}
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newFixedThreadPool(5);
List tasks = new ArrayList();
for (int i = 0; i < 5; i++) {
Task task = new Task("task" + i, i);
tasks.add(task);
}
List> results = executor.invokeAll(tasks);
for (int i = 0; i < results.size(); i++) {
Future result = results.get(i);
Result resultValue = result.get();
System.out.println(resultValue);
}
}
}
consle log
task0 begin********************* task1 begin********************* task0 end————————- task2 begin********************* task2 end————————- task3 begin********************* task4 begin********************* task4 end————————- Result [name=task0, value=number value is 0] Exception in thread “main” java.util.concurrent.ExecutionException: java.lang.Exception: task1the task failed! at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:188) at com.test.ExecutorExceptionTest.main(ExecutorExceptionTest.java:80) Caused by: java.lang.Exception: task1the task failed! at com.test.ExecutorExceptionTest Task.call(ExecutorExceptionTest.java:62)atcom.test.ExecutorExceptionTest Task.call(ExecutorExceptionTest.java:1) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)