美文网首页
多线程调用接口并拿到返回值

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

作者: LegendaryTao | 来源:发表于2019-12-12 10:42 被阅读0次

原理:开启线程,并实现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);
    }
}

相关文章

网友评论

      本文标题:多线程调用接口并拿到返回值

      本文链接:https://www.haomeiwen.com/subject/bvbzgctx.html