保证所有多线程都执行完,拿到最后所有线程的结果集

使用方法

Future

代码演示

public class AsyncQichacha {
    @Autowired
    private QichachaHttpClient qichachaHttpClient;

    @Async("qichacha")
    public Future findSpecifiedDataAsync(SearchParameters parameters, QichachaEnum qichachaEnum) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() +  " 线程启动");
        TimeUnit.SECONDS.sleep(3);
        System.out.println(Thread.currentThread().getName() +  " 线程结束");
        return new AsyncResult<>(qichachaHttpClient.findByQichacha(parameters, qichachaEnum));
    }
}
public class AsyncService {
    @Autowired
    private AsyncQichacha asyncQichacha;
    @Autowired
    private QichachaHttpClient qichachaHttpClient;

    public void AsyncRequest(SearchParameters parameters, Boolean details) throws InterruptedException, ExecutionException {
        List> list = new ArrayList();
        QichachaFullData refreshJson = new QichachaFullData();
        List courtNoticeDetailList = new ArrayList<>();

        /* 企业详细信息 */
        Future details1 = asyncQichacha.findSpecifiedDataAsync(parameters, QichachaEnum.QICHACHA_DETAILS);
        list.add(details1);

        /* 开庭公告列表信息 */
        Future courtNotice = asyncQichacha.findSpecifiedDataAsync(parameters, QichachaEnum.COURT_NOTICE);
        list.add(courtNotice);


        List result = new ArrayList<>();
        result.addAll(list);

        while (true){
            if (list.isEmpty()){
                break;
            }
            for (int i=0; i

你可能感兴趣的:(保证所有多线程都执行完,拿到最后所有线程的结果集)