页面修改线程池参数_2

除了修改核心线程数和最大线程数,实际中还经常需要修改阻塞队列长度,但是发现没有修改队列长度的方法,因为 LinkedBlockingQueue 的 capacity 被 final 修饰了。

我们可以按照这个思路自定义一个队列,让其可以对 capacity 参数进行修改即可。

操作起来也非常方便,把 LinkedBlockingQueue 粘贴一份出来,修改个名字,然后把 capacity 参数的 final 修饰符去掉,并提供其对应的 get/set 方法。

//拷贝一份LinkedBlockingQueue,改下capacity
public class MyLinkedBlockingQueue {

    private volatile int capacity;

    public int getCapacity() {
        return capacity;
    }
    
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    
    ...
}

public class ThreadChangeDemo {

    //创建
    private static ThreadPoolExecutor createExecutor(int corePoolSize,
                                                     int maximumPoolSize,
                                                     int queueCapacity){
        return new ThreadPoolExecutor(corePoolSize,
                maximumPoolSize,
                60,
                TimeUnit.SECONDS,
                new MyLinkedBlockingQueue<>(queueCapacity),
                new NamedThreadFactory("threadName"));
    }

    //修改
    private static void modifyExecutor(ThreadPoolExecutor executor,
                                       int corePoolSize,
                                       int maximumPoolSize,
                                       int queueCapacity){
        executor.setCorePoolSize(corePoolSize);
        executor.setMaximumPoolSize(maximumPoolSize);
        MyLinkedBlockingQueue queue = (MyLinkedBlockingQueue)executor.getQueue();
        queue.setCapacity(queueCapacity);
    }

    //查询
    private static void selectExecutor(ThreadPoolExecutor executor){
        System.out.println();
        System.out.print("线程:" + Thread.currentThread().getName());
        System.out.print(",");
        System.out.print("核心线程数:" + executor.getCorePoolSize());
        System.out.print(",");
        System.out.print("最大线程数:" + executor.getMaximumPoolSize());
        System.out.print(",");
        System.out.print("活动线程数:" + executor.getActiveCount());
        System.out.print(",");
        System.out.print("当前排队线程数:" + executor.getQueue().size());
        System.out.print(",");
        System.out.print("队列剩余大小:" + executor.getQueue().remainingCapacity());
        System.out.print(",");
        System.out.print("队列大小:" + (executor.getQueue().size() + executor.getQueue().remainingCapacity()));
        System.out.println();
    }

    //执行
    private static Future submitExecutor(ThreadPoolExecutor executor, Callable task){
        return executor.submit(task);
    }

    public static void main(String[] args) throws Exception {
        //创建
        ThreadPoolExecutor executor = createExecutor(2,2,9);
        selectExecutor(executor);

        //修改
        modifyExecutor(executor,10,10,10);
        selectExecutor(executor);

        //执行
        CountDownLatch countDownLatch = new CountDownLatch(3);
        for(int i = 0; i < 3; i++){
            Future future = submitExecutor(executor, () -> {
                System.out.println(Thread.currentThread().getName() + ":" + "begin");
                TimeUnit.SECONDS.sleep(10);
                String result = Thread.currentThread().getName() + "-" + "result";
                System.out.println(Thread.currentThread().getName() + ":" + "end");
                countDownLatch.countDown();
                return result;
            });
        }
        countDownLatch.await();
        System.out.println("end");
        selectExecutor(executor);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值