public class ThreadDemo {
public final ExecutorService threadPool = Executors.newCachedThreadPool();
public final CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(threadPool);
public static void main(String[] args) {
new ThreadDemo().solveCallableList();
}
public void solveCallableList() {
try {
int sum = 0;
completionService.submit(new TaskCallable(1));
completionService.submit(new TaskCallable(2));
completionService.submit(new TaskCallable(3));
for (int i = 0; i < 3; i++) {
int result = completionService.take().get();
sum += result;
}
System.out.println("sum = " + sum);
} catch (Exception e) {
e.printStackTrace();
}
}
public class TaskCallable implements Callable<Integer> {
private int options;
TaskCallable(int options) {
this.options = options;
}
@Override
public Integer call() throws Exception {
switch (options) {
case 1:
return Integer.valueOf(2 / 0);
case 2:
return Integer.valueOf(2 + 1);
case 3:
return Integer.valueOf(2 - 1);
default:
break;
}
return 0;
}
}
}

解决方案 »

  1.   

    这是原来的代码,用了Executors的invokeAll方法,如果其中有一个任务有错误,那么就会抛出异常,但是我还想获得其他的结果值。
    public class ThreadDemo { public final ExecutorService threadPool = Executors.newCachedThreadPool();
    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new ThreadDemo().solveCallableList();
    } public void solveCallableList() {
    try {
    int sum = 0;
    List<Callable<Integer>> callableList = new ArrayList<Callable<Integer>>();
    callableList.add(new TaskCallable(1));
    callableList.add(new TaskCallable(2));
    callableList.add(new TaskCallable(3));
    List<Future<Integer>> futureList = threadPool.invokeAll(callableList, 2000L, TimeUnit.MILLISECONDS);
    if (futureList != null && futureList.size() > 0) {
    for (Future<Integer> future : futureList) {
    int result = future.get();
    sum += result;
    }
    }
    System.out.println("sum = " + sum);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public class TaskCallable implements Callable<Integer> {
    private int options; TaskCallable(int options) {
    this.options = options;
    } @Override
    public Integer call() throws Exception {
    try{
    switch (options) {
    case 1:
    return Integer.valueOf(2 / 0);
    case 2:
    return Integer.valueOf(2 + 1);
    case 3:
    return Integer.valueOf(2 - 1);
    default:
    break;
    }
    }catch(Exception e){
    e.printStackTrace();
    }
    return null;
    }
    }
    }这是改进之后的
    public class ThreadDemo1 {
    public final ExecutorService threadPool = Executors.newCachedThreadPool();
    public final CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(threadPool); public static void main(String[] args) {
    new ThreadDemo1().solveCallableList();
    } public void solveCallableList() {
    try {
    int sum = 0;
    completionService.submit(new TaskCallable(1));
    completionService.submit(new TaskCallable(2));
    completionService.submit(new TaskCallable(3));
    for (int i = 0; i < 3; i++) {
    int result = completionService.take().get();
    sum += result;
    }
    System.out.println("sum = " + sum);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public class TaskCallable implements Callable<Integer> {
    private int options; TaskCallable(int options) {
    this.options = options;
    } @Override
    public Integer call() throws Exception {
    try {
    switch (options) {
    case 1:
    return Integer.valueOf(2 / 0);
    case 2:
    return Integer.valueOf(2 + 1);
    case 3:
    return Integer.valueOf(2 - 1);
    default:
    break;
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return 0;
    }
    }
    }改进之后用ExecutorCompletionService可以获取其他的两个任务的结果值。