线程池的使用

示例1:

package com.xx.job.day02;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @ClassName ThreadPoolTest
 * @Description
 * @Author xuxiang
 * @Date 11:46 2023/3/28
 **/
@Slf4j(topic = "c.ThreadPoolTest")
public class ThreadPoolTest01 {

    private static BlockingQueue blockingQueue1 = new ArrayBlockingQueue(100); // 有界
    private static BlockingQueue blockingQueue2 = new SynchronousQueue();              // 为0
    private static BlockingQueue blockingQueue3 = new LinkedBlockingDeque(100);// 默认无界,支持有界
    private static BlockingQueue blockingQueue4 = new LinkedTransferQueue();           //无界
    private static BlockingQueue blockingQueue5 = new LinkedBlockingQueue(100);           // 默认无界,支持有界
    private static BlockingQueue blockingQueue6 = new PriorityBlockingQueue();         // 无界队列

    public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20,
            3000, TimeUnit.MICROSECONDS, blockingQueue1, new ThreadPoolExecutor.AbortPolicy());
    public static void main(String[] args) throws InterruptedException {
        ConcurrentLinkedQueue<Integer> list = new ConcurrentLinkedQueue<>();
        final CountDownLatch countDownLatch = new CountDownLatch(1000);
        for (int i = 0; i < 1000; i++) {
            log.debug("第{}个任务", i);
            try {
                FutureTask test = test(i, list,countDownLatch);
                threadPoolExecutor.submit(test);
            } catch (RejectedExecutionException e) {
                e.printStackTrace();;
                log.error("拒绝执行{}", i);
                Thread.sleep(1000);
                log.debug("继续执行{}", i);
                FutureTask test = test(i,list,countDownLatch);
                threadPoolExecutor.submit(test);
            }
        }
        countDownLatch.await();
        // threadPoolExecutor.shutdown();
        log.info("list size:{},list:{}", list.size(), JSONUtil.toJsonStr(list));
    }


    public static FutureTask test(int index, ConcurrentLinkedQueue<Integer> list,CountDownLatch countDownLatch){
        FutureTask futureTask = new FutureTask(() -> {
            try {
                Thread.sleep(100);
                list.add(index);
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                log.error("InterruptedException", e);
            }
            log.debug("test run {}", index);
            return null;
        });
        log.debug("队列剩余空间:{}", blockingQueue6.remainingCapacity());
        return futureTask;
    }
}

示例2:

package com.xx.job.day02;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;

/**
 * @ClassName ThreadPoolTest
 * @Description
 * @Author xuxiang
 * @Date 11:46 2023/3/28
 **/
@Slf4j(topic = "c.ThreadPoolTest")
public class ThreadPoolTest {

    private static BlockingQueue blockingQueue1 = new ArrayBlockingQueue(100); // 有界
    private static BlockingQueue blockingQueue2 = new SynchronousQueue();              // 为0
    private static BlockingQueue blockingQueue3 = new LinkedBlockingDeque(100);// 默认无界,支持有界
    private static BlockingQueue blockingQueue4 = new LinkedTransferQueue();           //无界
    private static BlockingQueue blockingQueue5 = new LinkedBlockingQueue(100);           // 默认无界,支持有界
    private static BlockingQueue blockingQueue6 = new PriorityBlockingQueue();         // 无界队列

    public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20,
            3000, TimeUnit.MICROSECONDS, blockingQueue1, new ThreadPoolExecutor.AbortPolicy());
    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch countDownLatch = new CountDownLatch(1000);
        ConcurrentLinkedQueue<Integer> list = new ConcurrentLinkedQueue<>();
        for (int i = 0; i < 1000; i++) {
            log.debug("第{}个任务", i);
            try {
                test(i,list,countDownLatch);
            } catch (RejectedExecutionException e) {
                e.printStackTrace();;
                log.error("拒绝执行{}", i);
                Thread.sleep(1000);
                log.debug("继续执行{}", i);
                test(i,list,countDownLatch);
            }
        }
        countDownLatch.await();
        // threadPoolExecutor.shutdown();
        log.info("list size:{},list:{}", list.size(), JSONUtil.toJsonStr(list));
    }


    public static void test(int index, ConcurrentLinkedQueue<Integer> list,CountDownLatch countDownLatch){
        threadPoolExecutor.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                    list.add(index);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    log.error("InterruptedException", e);
                }
                log.debug("test run {}", index);

            }
        });
            log.debug("队列剩余空间:{}", blockingQueue6.remainingCapacity());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值