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

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

1.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@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@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@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@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@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@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@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@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@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@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@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@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
    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@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@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@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@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@36be2ddf当前时间:Wed Nov 14 17:16:07 CST 2018

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

猜你喜欢

转载自blog.csdn.net/u010520146/article/details/84070175