首页 > 编程语言 > 详细

使用线程池获取结果,CountDownLatch+ThreadPool,FutureTask+ThreadPool 并比较

时间:2021-09-15 19:14:46      阅读:6      评论:0      收藏:0      [点我收藏+]

CountDownLatch+ThreadPool代码

线程代码:

package com.tuniu.app.thread;

import com.tuniu.BillElectronicItemAttachment;

import java.util.List;
import java.util.concurrent.CountDownLatch;

public class UploadImageThread implements Runnable{
    public static final String IMAGE_START = "image/";
    private List<BillElectronicItemAttachment> resultList;
    private CountDownLatch countDownLatch;
    private String paramStr;

    public UploadImageThread(List<BillElectronicItemAttachment> resultList, CountDownLatch countDownLatch,String paramStr) {
        this.resultList = resultList;
        this.countDownLatch = countDownLatch;
        this.paramStr = paramStr;
    }

    @Override
    public void run() {
        try {
            BillElectronicItemAttachment billElectronicItemAttachment = new BillElectronicItemAttachment();
            billElectronicItemAttachment.setFileName(paramStr);
            resultList.add(billElectronicItemAttachment);
            Thread.sleep(2);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            countDownLatch.countDown();
        }
    }
}

 

测试方法:

    private static void countThreadTest(ThreadPoolExecutor threadPoolExecutor, CountDownLatch countDownLatch, List<BillElectronicItemAttachment> list) throws InterruptedException {
        for (int i = 0; i < 2000; i++) {
            Integer v = (int) (Math.random() * 100000);
            threadPoolExecutor.execute(new UploadImageThread(list, countDownLatch, v.toString()));
        }
        countDownLatch.await();
    }

FutureTask+ThreadPool 代码:

线程代码:

package com.tuniu.app.thread;

import java.util.concurrent.Callable;

public class UploadImageThreadSubmit implements Callable<String> {
    private String paramStr;

    public UploadImageThreadSubmit(String paramStr) {
        this.paramStr = paramStr;
    }

    @Override
    public String call() throws InterruptedException {
        Thread.sleep(2);
        return paramStr;
    }
}

测试方法:

    private static void callableTest(ThreadPoolExecutor threadPoolExecutor, List<BillElectronicItemAttachment> list) throws ExecutionException, InterruptedException {
        List<FutureTask> futureTasks = new ArrayList<FutureTask>();
        for (int i = 0; i < 2000; i++) {
            Integer v = (int) (Math.random() * 100000);
            FutureTask<String> stringFutureTask = new FutureTask<String>(new UploadImageThreadSubmit(v.toString()));
            threadPoolExecutor.execute(stringFutureTask);
            futureTasks.add(stringFutureTask);
        }
        for (FutureTask futureTask : futureTasks) {
            BillElectronicItemAttachment billElectronicItemAttachment = new BillElectronicItemAttachment();
            billElectronicItemAttachment.setFileName((String) futureTask.get());
            list.add(billElectronicItemAttachment);
        }
    }

测试代码:

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        for (int i = 0; i < 20; i++) {
            long start = System.currentTimeMillis();
            ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 8, 10000L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(100), new ThreadPoolExecutor.CallerRunsPolicy());
            CountDownLatch countDownLatch = new CountDownLatch(20);
            List<BillElectronicItemAttachment> list = Collections.synchronizedList(new ArrayList<BillElectronicItemAttachment>());
            countThreadTest(threadPoolExecutor, countDownLatch, list);
//            callableTest(threadPoolExecutor, list);
            System.out.println("我结束了:" + (System.currentTimeMillis() - start));
            threadPoolExecutor.shutdown();
        }

    }

结果对比:

技术分享图片技术分享图片

 

 

结论:

  CountDownLatch+ThreadPool稍快,无论是第一次执行,还是后续的执行

原因:

  

 

使用线程池获取结果,CountDownLatch+ThreadPool,FutureTask+ThreadPool 并比较

原文:https://www.cnblogs.com/lzghyh/p/15268494.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!