Java

java 并发 几种线程池的基本使用

2020-05-08  本文已影响0人  静享时光

Java中主要有四种线程池:

1、newCachedThreadPool()
创建一个可缓存线程,如果线程池长度超过处理需要,可灵活回收空闲线程,如果没有可回收的,则新建线程
2、newFixedThreadPool()
创建一个定长的线程池,可控制最大并发数,超出的线程会在队列中等待执行
3、newSingleThreadExecutor()
创建一个单线程化的线程池,只用唯一的一个工作线程执行任务。可以保证所有的任务都按照添加的顺序执行
4、newScheduledThreadPool()
创建一个定长线程池,支持定时及周期性执行任务。
schedule()方法执行定时任务
scheduleAtFixedRate()方法执行周期性的任务

四种线程池的基本使用

class MyRunnable implements Runnable {
    private int countDown = 10;

    private static int taskCount = 0;
    private final int id = taskCount++;

    public MyRunnable(int countDown) {
        this.countDown = countDown;
    }

    private String status() {
        return "#" + id + "(" + (countDown > 0 ? countDown : "countDown<=0") + ")  ";
    }

    @Override
    public void run() {
        while (countDown-- > 0) {
            System.out.print(status());
//            Thread.yield();
            if (countDown == 0) {
                System.out.println();
            }
        }

    }
}

public class ThreadTest1 {
    public static void main(String[] args) {

        //创建一个可缓存线程,如果线程池长度超过处理需要,可灵活回收空闲线程,如果没有可回收的,则新建线程
        ExecutorService executorService1 = Executors.newCachedThreadPool();
        for (int i = 0; i < 6; i++) {
            executorService1.execute(new MyRunnable(5));
        }
        /**
         * 输出
         *
         * #1(4)  #2(4)  #0(4)  #2(3)  #1(3)  #5(4)  #1(2)  #3(4)  #1(1)  #5(3)  #2(2)  #0(3)  #4(4)  #0(2)  #2(1)  #5(2)  #1(countDown<=0)  #3(3)
         * #5(1)  #2(countDown<=0)
         * #0(1)  #4(3)  #0(countDown<=0)
         * #5(countDown<=0)
         * #3(2)  #4(2)  #3(1)  #4(1)  #3(countDown<=0)
         * #4(countDown<=0)
         *
         *
         */


        //创建一个定长的线程池,可控制最大并发数,超出的线程会在队列中等待执行
        ExecutorService executorService2 = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 6; i++) {
            executorService2.execute(new MyRunnable(5));
        }

        /**
         * 输出
         *
         * #1(4)  #0(4)  #2(4)  #0(3)  #1(3)  #0(2)  #2(3)  #0(1)  #1(2)  #0(countDown<=0)  #2(2)
         * #2(1)  #1(1)  #3(4)  #2(countDown<=0)
         * #3(3)  #1(countDown<=0)
         * #3(2)  #4(4)  #5(4)  #3(1)  #5(3)  #4(3)  #5(2)  #3(countDown<=0)
         * #5(1)  #4(2)  #5(countDown<=0)
         * #4(1)  #4(countDown<=0)
         *
         *
         */

        //创建一个单线程化的线程池,只用唯一的一个工作线程执行任务。
        //可以保证所有的任务都按照添加的顺序执行
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            executorService.execute(new MyRunnable(5));
        }
        /**
         * 日志打印
         *
         * #0(4)  #0(3)  #0(2)  #0(1)  #0(countDown<=0)
         * #1(4)  #1(3)  #1(2)  #1(1)  #1(countDown<=0)
         * #2(4)  #2(3)  #2(2)  #2(1)  #2(countDown<=0)
         * #3(4)  #3(3)  #3(2)  #3(1)  #3(countDown<=0)
         * #4(4)  #4(3)  #4(2)  #4(1)  #4(countDown<=0)
         * #5(4)  #5(3)  #5(2)  #5(1)  #5(countDown<=0)
         * #6(4)  #6(3)  #6(2)  #6(1)  #6(countDown<=0)
         * #7(4)  #7(3)  #7(2)  #7(1)  #7(countDown<=0)
         * #8(4)  #8(3)  #8(2)  #8(1)  #8(countDown<=0)
         * #9(4)  #9(3)  #9(2)  #9(1)  #9(countDown<=0)
         *
         */

        //创建一个定长线程池,支持定时及周期性执行任务。
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
        //延迟执行任务
        scheduledThreadPool.schedule(new Runnable() {

            @Override
            public void run() {
                System.out.println("delay 3 seconds");
            }
        }, 3, TimeUnit.SECONDS);

        //周期性执行任务
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                System.out.println("delay 1 seconds, and excute every 3 seconds");
            }
        }, 1, 3, TimeUnit.SECONDS);
    }
}
上一篇下一篇

猜你喜欢

热点阅读