自定线程池测试

package com.example.demo.test.ThreadPool;

import java.util.concurrent.*;

public class ThreadPoolTest {

    // 核心线程池大小
    private int corePoolSize = 1;

    // 最大可创建的线程数
    private int maxPoolSize = 3;

    // 队列最大长度
    private int queueCapacity = 3;

    // 线程池维护线程所允许的空闲时间
    private int keepAliveSeconds = 30;

    private ThreadPoolExecutor getExcutor(){
        BlockingDeque<Runnable> blockingDeque =  new LinkedBlockingDeque<Runnable>(queueCapacity);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,maxPoolSize,keepAliveSeconds, TimeUnit.SECONDS,blockingDeque,new ThreadPoolExecutor.CallerRunsPolicy());
        return threadPoolExecutor;
    }

    public static void main(String[] args) {
        ThreadPoolTest threadPoolTest = new ThreadPoolTest();
        ThreadPoolExecutor threadPoolExecutor =   threadPoolTest.getExcutor();
        final Integer m = 0;
        for(int i = 0;i<10;i++){
            int finalI = i;
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("come in"+ finalI);
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("come out"+ finalI);
                }
            });
        }
        threadPoolExecutor.shutdown();
    }
}

结果:

come in0
come in4
come in5
come in6
come out0
come out4
come out5
come out6
come in1
come in3
come in2
come out1
come out2
come out3
come in8
come in7
come in9
come out7
come out9
come out8

 

不多废话,拒绝策略的影响自己测试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值