获取线程执行结果

如果线程执行过程中抛出异常,在获取结果时会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 {

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<Result> {

    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<Task> tasks = new ArrayList<Task>();
    for (int i = 0; i < 5; i++) {
        Task task = new Task("task" + i, i);
        tasks.add(task);
    }
    List<Future<Result>> results = executor.invokeAll(tasks);

    for (int i = 0; i < results.size(); i++) {
        Future<Result> 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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值