CachedThreadPool与FixedThreadPool

CachedThreadPool在超时60s后会移除线程,适合短期异步任务;FixedThreadPool保持固定数量线程,适合稳定并发任务,无超时机制。
摘要由CSDN通过智能技术生成

1.一个线程实例加入CachedThreadPool时,如果该线程实例不存在CachedThreadPool,则创建一个,CachedThreadPool中线程实例默认超时时间为60s,超过这个时间,线程实例停止并被移出CachedThreadPool,适用于生存期短、异步的线程任务。

2.FixedThreadPool中最多只有固定数目线程存在,一个线程实例请求加入FixedThreadPool时,如果该实例不存在,且没有达到线程池数目上线,则会创建一个实例,否则,会先加入等待序列,当FixedThreadPool中有一个线程停止并移出线程池后,线程实例才能加入线程池,FixedThreadPool没有超时机制,适用于稳定且并发线程任务

3.public static void main(String[] args) {

        int pool_size = 10;
        ExecutorService executorService = Executors.newCachedThreadPool();
        int cpuNums = Runtime.getRuntime().availableProcessors();
        ExecutorService executorService1 = Executors.newFixedThreadPool(cpuNums * pool_size);
        int i = 0;
        while (true) {
            System.out.println(++i);
            executorService1.execute(new Handler());//将实例加入线程池
        }
    }


    public static class Handler implements Runnable {
        public void run() {
            System.out.println("----");
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值