java线程池详细讲解代码篇(二)

目录

线程池实现原理

1:使用工具类Executors来生成各种不同线程池

2:底层都是通过new ThreadPoolExecutor来实现的

3:核心方法

几种线程池的测试

1:newFixedThreadPool

2:newSingleThreadExecutor

3:newCachedThreadPool

 


线程池实现原理

1:使用工具类Executors来生成各种不同线程池

2:底层都是通过new ThreadPoolExecutor来实现的

3:核心方法

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

 

几种线程池的测试(附加测试结果)

1:newFixedThreadPool

底层使用的阻塞队列是 LinkedBlockingQueue<Runnable>()

/**
 * newFixedThreadPool
 * 固定大小线程池
 */
@Test
public void testFix(){
    ExecutorService threadPool = Executors.newFixedThreadPool(3);
    for (int i = 1; i <= 10; i++) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "\t 工作");
            }
        });
    }
}

结果: 最多会出现3个
pool-1-thread-1	 工作
pool-1-thread-2	 工作
pool-1-thread-3	 工作
pool-1-thread-1	 工作
pool-1-thread-2	 工作
pool-1-thread-3	 工作
pool-1-thread-1	 工作
pool-1-thread-3	 工作
pool-1-thread-2	 工作
pool-1-thread-1	 工作

2:newSingleThreadExecutor

底层使用的阻塞队列是 LinkedBlockingQueue<Runnable>()

/**
 * newSingleThreadExecutor
 * 1个大小线程池
 */
@Test
public void testSingle(){
    ExecutorService threadPool = Executors.newSingleThreadExecutor();
    for (int i = 1; i <= 10; i++) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "\t 工作");
            }
        });
    }
}

结果: 永远只有一个线程在工作
pool-1-thread-1	 工作
pool-1-thread-1	 工作
pool-1-thread-1	 工作
pool-1-thread-1	 工作
pool-1-thread-1	 工作
pool-1-thread-1	 工作
pool-1-thread-1	 工作
pool-1-thread-1	 工作
pool-1-thread-1	 工作
pool-1-thread-1	 工作

3:newCachedThreadPool

底层使用的阻塞队列是 SynchronousQueue<Runnable>()

/**
 * newCachedThreadPool
 * 自适应大小线程池
 */
@Test
public void testCached(){
    ExecutorService threadPool = Executors.newCachedThreadPool();
    for (int i = 1; i <= 20; i++) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "\t 工作");
            }
        });
    }
}
结果:工作线程的数量是可以自适应扩展的,读者可以调整任务个数进行测试
pool-1-thread-1	 工作
pool-1-thread-2	 工作
pool-1-thread-3	 工作
pool-1-thread-2	 工作
pool-1-thread-1	 工作
pool-1-thread-3	 工作
pool-1-thread-2	 工作
pool-1-thread-3	 工作
pool-1-thread-2	 工作
pool-1-thread-3	 工作
pool-1-thread-3	 工作
pool-1-thread-3	 工作
pool-1-thread-1	 工作
pool-1-thread-2	 工作
pool-1-thread-1	 工作
pool-1-thread-2	 工作
pool-1-thread-1	 工作
pool-1-thread-2	 工作
pool-1-thread-3	 工作
pool-1-thread-1	 工作

 

执行完需要手动关闭线程池

threadPool.shutdown();

java线程池详细讲解理论篇(一) https://blog.csdn.net/weixin_45191798/article/details/100153524

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值