多线程调用接口并拿到返回值

原理:开启线程,并实现Callable接口
一、线程池

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池
 * 
 * @author ht
 * 
 */
public class ThreadPoolEx {
    public static ThreadPoolExecutor tPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(3);

    /**
     * 初始化线程池
     * 
     */
    public ThreadPoolEx() {
    }

    public static ThreadPoolExecutor getInstance() {
        if (tPool == null) {
            tPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();
        }

        return tPool;
    }
}

二、代码执行

 private ThreadPoolExecutor pool = ThreadPoolEx.getInstance();

    /**
     * 循环--拉取并存库
     *  @param totalPages
     * @param startDate
     * @param endDate
     */
    private void getRequestPriceChange(Integer totalPages, String startDate, String endDate){
        List<Future<List<T>>> futureList = new ArrayList<>();

        for (int currentPageIndex = 2; currentPageIndex <= totalPages; currentPageIndex++) {
            PullChangePriceTask priceTask = new PullChangePriceTask(pool, this, startDate, endDate, currentPageIndex);
            Future<List<T>> roomPriceInfoFuture = pool.submit(priceTask);
            futureList.add(roomPriceInfoFuture);
        }

        List<T> rateChangeInfos = new ArrayList<>();
        for (Future<List<T>> ctripResponseFuture : futureList) {
            List<T> rateChangeInfo = null;
            try {
                rateChangeInfo = ctripResponseFuture.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
            rateChangeInfos.addAll(rateChangeInfo);
        }
    }

三、线程

import com.ctrip.entity.response.hotel.pice.change.RateChangeInfo;
import com.ctrip.spring.utils.exception.MsgException;
import com.ctrip.web.manage.controller.hotel.PriceController;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: Jr
 * @Description:
 * @Date:Createc in 上午11:34 2018/11/27
 * @Modified By:
 */
public class PullChangePriceTask implements Callable<List<RateChangeInfo>> {

    ThreadPoolExecutor taskExecutor;

    private PriceController priceController;

    private String startDate;

    private String endDate;

    private int currentPageIndex;

    public PullChangePriceTask(ThreadPoolExecutor taskExecutor, PriceController priceController, String startDate, String endDate, int currentPageIndex) {
        this.taskExecutor = taskExecutor;
        this.priceController = priceController;
        this.startDate = startDate;
        this.endDate = endDate;
        this.currentPageIndex = currentPageIndex;
    }

    @Override
    public List<RateChangeInfo> call() {
       return priceController.pullChangePriceAndSave(startDate,endDate,currentPageIndex);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 线程池ThreadPoolExecutor corepoolsize:核心池的大小,默认情况下,在创建了线程池之后...
    irckwk1阅读 772评论 0 0
  • 林炳文Evankaka原创作品。转载自http://blog.csdn.net/evankaka 本文主要讲了ja...
    ccq_inori阅读 671评论 0 4
  • 先看几个概念:线程:进程中负责程序执行的执行单元。一个进程中至少有一个线程。多线程:解决多任务同时执行的需求,合理...
    yeying12321阅读 573评论 0 0
  • 若干年后,有一魔从遥远的宇宙来到地球。它疯狂肆虐,与地球人发动了一场战争,掀起一场血雨腥风。地球人竭尽全力,把所有...
    新雪00阅读 447评论 -1 3
  • 一、学生阅读现状分析 二年级学生已经较为系统的对阅读有了一年的认识,阅读习惯正在逐步养成,大部分学生也都开始喜欢看...
    洮北699高惠婷阅读 469评论 2 4