Java并发编程之线程池(三)

一.介绍
Java通过Executors提供四种线程池,分别为:
(1)newCachedThreadPool:创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
(2)newFixedThreadPool: 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
(3)newScheduledThreadPool :创建一个定长线程池,支持定时及周期性任务执行。
(4)newSingleThreadExecutor: 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
二.基本使用

1.newCachedThreadPool

创建一个可缓存线程池,使用newCachedThreadPool的时候要根据实际任务运行时间情况,因为它可以创建最多Integer.MAX_VALUE个线程,反而会占用系统资源,降低运行效率。这就是为什么官方文档中会说:newCachedThreadPool会大幅度提高大量短暂异步任务的性能了

    public static class TestRunable implements Runnable {
        int index;
        public TestRunable(int i){
            this.index=i;
        }
        @Override
        public void run() {
            System.out.println("我的线程"+index+":"+this.toString());
        }
    }

    public static void main(String[] args) {
        ExecutorService newCachedThreadPool = newCachedThreadPool();
        for(int i=0;i<10;i++) {
            newCachedThreadPool.execute(new TestRunable(i));
        }
    }

结果:

我的线程0:concurrency.ThreadPoolTest T e s t R u n a b l e @ 4 a 24 a 59 我 的 线 程 1 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@4a24a59 我的线程1:concurrency.ThreadPoolTest TestRunable@4a24a59线1:concurrency.ThreadPoolTestTestRunable@72f5c249
我的线程2:concurrency.ThreadPoolTest T e s t R u n a b l e @ 57 a 0 e 8 e a 我 的 线 程 3 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@57a0e8ea 我的线程3:concurrency.ThreadPoolTest TestRunable@57a0e8ea线3:concurrency.ThreadPoolTestTestRunable@7ad06c3f
我的线程4:concurrency.ThreadPoolTest T e s t R u n a b l e @ 797372 a c 我 的 线 程 5 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@797372ac 我的线程5:concurrency.ThreadPoolTest TestRunable@797372ac线5:concurrency.ThreadPoolTestTestRunable@128abd43
我的线程6:concurrency.ThreadPoolTest T e s t R u n a b l e @ 68422010 我 的 线 程 7 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@68422010 我的线程7:concurrency.ThreadPoolTest TestRunable@68422010线7:concurrency.ThreadPoolTestTestRunable@5644a213
我的线程8:concurrency.ThreadPoolTest T e s t R u n a b l e @ 3 b 3595 c 8 我 的 线 程 9 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@3b3595c8 我的线程9:concurrency.ThreadPoolTest TestRunable@3b3595c8线9:concurrency.ThreadPoolTestTestRunable@f710c85

2.newFixedThreadPool
 public static class TestRunable implements Runnable {
        int index;
        public TestRunable(int i){
            this.index=i;
        }
        @Override
        public void run() {
            System.out.println("我的线程"+index+":"+this.toString()+"当前时间:"+new Date());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ExecutorService newCachedThreadPool = newFixedThreadPool(3);
        for(int i=0;i<10;i++) {
            newCachedThreadPool.execute(new TestRunable(i));
        }
    }

结果:

我的线程1:concurrency.ThreadPoolTest T e s t R u n a b l e @ 2 b 3 b 219 b 当 前 时 间 : W e d N o v 1416 : 37 : 08 C S T 2018 我 的 线 程 0 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@2b3b219b当前时间:Wed Nov 14 16:37:08 CST 2018 我的线程0:concurrency.ThreadPoolTest TestRunable@2b3b219b:WedNov1416:37:08CST2018线0:concurrency.ThreadPoolTestTestRunable@e1604df当前时间:Wed Nov 14 16:37:08 CST 2018
我的线程2:concurrency.ThreadPoolTest T e s t R u n a b l e @ 53 b 69 d 37 当 前 时 间 : W e d N o v 1416 : 37 : 08 C S T 2018 我 的 线 程 3 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@53b69d37当前时间:Wed Nov 14 16:37:08 CST 2018 我的线程3:concurrency.ThreadPoolTest TestRunable@53b69d37:WedNov1416:37:08CST2018线3:concurrency.ThreadPoolTestTestRunable@25fcda8c当前时间:Wed Nov 14 16:37:09 CST 2018
我的线程4:concurrency.ThreadPoolTest T e s t R u n a b l e @ 7067 b 35 a 当 前 时 间 : W e d N o v 1416 : 37 : 09 C S T 2018 我 的 线 程 5 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@7067b35a当前时间:Wed Nov 14 16:37:09 CST 2018 我的线程5:concurrency.ThreadPoolTest TestRunable@7067b35a:WedNov1416:37:09CST2018线5:concurrency.ThreadPoolTestTestRunable@5d717931当前时间:Wed Nov 14 16:37:09 CST 2018
我的线程6:concurrency.ThreadPoolTest T e s t R u n a b l e @ 70339 e e 当 前 时 间 : W e d N o v 1416 : 37 : 10 C S T 2018 我 的 线 程 7 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@70339ee当前时间:Wed Nov 14 16:37:10 CST 2018 我的线程7:concurrency.ThreadPoolTest TestRunable@70339ee:WedNov1416:37:10CST2018线7:concurrency.ThreadPoolTestTestRunable@21c34aeb当前时间:Wed Nov 14 16:37:10 CST 2018
我的线程8:concurrency.ThreadPoolTest T e s t R u n a b l e @ 594 e f 647 当 前 时 间 : W e d N o v 1416 : 37 : 10 C S T 2018 我 的 线 程 9 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@594ef647当前时间:Wed Nov 14 16:37:10 CST 2018 我的线程9:concurrency.ThreadPoolTest TestRunable@594ef647:WedNov1416:37:10CST2018线9:concurrency.ThreadPoolTestTestRunable@21c30e19当前时间:Wed Nov 14 16:37:11 CST 2018

可以发现每次只进入三个进场执行,其他线程等待,当前面的线程执行完毕后后面的线程进入.

3.newScheduledThreadPool

创建固定大小且能够执行定时或周期性任务的线程池

   public static class TestRunable implements Runnable {
        int index;
        public TestRunable(int i){
            this.index=i;
        }
        @Override
        public void run() {
            System.out.println("我的线程"+index+":"+this.toString()+"当前时间:"+new Date());
        }
    }

    public static void main(String[] args) {
        ScheduledExecutorService executorService = newScheduledThreadPool(3);
        System.out.println("当前时间:"+new Date());
        for(int i=0;i<5;i++) {
            executorService.schedule(new TestRunable(i),3, TimeUnit.SECONDS);
        }
    }

结果:

当前时间:Wed Nov 14 17:13:08 CST 2018
我的线程1:concurrency.ThreadPoolTest T e s t R u n a b l e @ 775 d f 5 d b 当 前 时 间 : W e d N o v 1417 : 13 : 11 C S T 2018 我 的 线 程 2 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@775df5db当前时间:Wed Nov 14 17:13:11 CST 2018 我的线程2:concurrency.ThreadPoolTest TestRunable@775df5db:WedNov1417:13:11CST2018线2:concurrency.ThreadPoolTestTestRunable@3272e778当前时间:Wed Nov 14 17:13:11 CST 2018
我的线程3:concurrency.ThreadPoolTest T e s t R u n a b l e @ 799 a 0540 当 前 时 间 : W e d N o v 1417 : 13 : 11 C S T 2018 我 的 线 程 4 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@799a0540当前时间:Wed Nov 14 17:13:11 CST 2018 我的线程4:concurrency.ThreadPoolTest TestRunable@799a0540:WedNov1417:13:11CST2018线4:concurrency.ThreadPoolTestTestRunable@3cb51f46当前时间:Wed Nov 14 17:13:11 CST 2018
我的线程0:concurrency.ThreadPoolTest$TestRunable@b7d228b当前时间:Wed Nov 14 17:13:11 CST 2018

通过结果可看出,先延时3秒后执行线程,除了延迟执行之外和newFixedThreadPool基本相同,可以用来执行定时任务

4.newSingleThreadExecutor

创建单线程的线程池 ,当该单线程在shutdown之前由于失败而终止时,将会有新的线程来代替它执行剩下任务。加入到该线程池里的线程会按顺序执行,一个时刻保证只有一个线程在运行

    public static class TestRunable implements Runnable {
        int index;
        public TestRunable(int i){
            this.index=i;
        }
        @Override
        public void run() {
            System.out.println("我的线程"+index+":"+this.toString()+"当前时间:"+new Date());
        }
    }

    public static void main(String[] args) {
        ExecutorService newSingleThreadExecutor = newSingleThreadExecutor();
        System.out.println("当前时间:"+new Date());
        for(int i=0;i<10;i++) {
            newSingleThreadExecutor.execute(new TestRunable(i));
        }
    }

结果:

当前时间:Wed Nov 14 17:16:07 CST 2018
我的线程0:concurrency.ThreadPoolTest T e s t R u n a b l e @ 2 d 992 b 37 当 前 时 间 : W e d N o v 1417 : 16 : 07 C S T 2018 我 的 线 程 1 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@2d992b37当前时间:Wed Nov 14 17:16:07 CST 2018 我的线程1:concurrency.ThreadPoolTest TestRunable@2d992b37:WedNov1417:16:07CST2018线1:concurrency.ThreadPoolTestTestRunable@235ec0f4当前时间:Wed Nov 14 17:16:07 CST 2018
我的线程2:concurrency.ThreadPoolTest T e s t R u n a b l e @ 27614047 当 前 时 间 : W e d N o v 1417 : 16 : 07 C S T 2018 我 的 线 程 3 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@27614047当前时间:Wed Nov 14 17:16:07 CST 2018 我的线程3:concurrency.ThreadPoolTest TestRunable@27614047:WedNov1417:16:07CST2018线3:concurrency.ThreadPoolTestTestRunable@6263cacf当前时间:Wed Nov 14 17:16:07 CST 2018
我的线程4:concurrency.ThreadPoolTest T e s t R u n a b l e @ 6 c 79527 当 前 时 间 : W e d N o v 1417 : 16 : 07 C S T 2018 我 的 线 程 5 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@6c79527当前时间:Wed Nov 14 17:16:07 CST 2018 我的线程5:concurrency.ThreadPoolTest TestRunable@6c79527:WedNov1417:16:07CST2018线5:concurrency.ThreadPoolTestTestRunable@534bac6b当前时间:Wed Nov 14 17:16:07 CST 2018
我的线程6:concurrency.ThreadPoolTest T e s t R u n a b l e @ 7 e 05 a d 3 d 当 前 时 间 : W e d N o v 1417 : 16 : 07 C S T 2018 我 的 线 程 7 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@7e05ad3d当前时间:Wed Nov 14 17:16:07 CST 2018 我的线程7:concurrency.ThreadPoolTest TestRunable@7e05ad3d:WedNov1417:16:07CST2018线7:concurrency.ThreadPoolTestTestRunable@24c3580当前时间:Wed Nov 14 17:16:07 CST 2018
我的线程8:concurrency.ThreadPoolTest T e s t R u n a b l e @ 3818 e 643 当 前 时 间 : W e d N o v 1417 : 16 : 07 C S T 2018 我 的 线 程 9 : c o n c u r r e n c y . T h r e a d P o o l T e s t TestRunable@3818e643当前时间:Wed Nov 14 17:16:07 CST 2018 我的线程9:concurrency.ThreadPoolTest TestRunable@3818e643:WedNov1417:16:07CST2018线9:concurrency.ThreadPoolTestTestRunable@36be2ddf当前时间:Wed Nov 14 17:16:07 CST 2018

可以看出按顺序执行每次只执行一个线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值