线程池

线程池

在执行一个异步或并发的任务时,往往是通过直接 new Thread() 方法来创建新的线程,这样做弊端较多,每次都要手动的去new,用完以后还要手动的销毁,所以就引入了线程池,线程池的优势

为什么使用线程池

1、降低系统资源消耗,通过重用已存在的线程,降低线程创建和销毁造成的消耗。

2、提高系统响应速度,当有任务到达时,无需等待新线程的创建便能立即执行;

3、线程池可以有效管控线程,统一分配、调优、提高资源使用率;方便线程的管理

4、使用方便简单,而且功能强大,线程池提供了定时、定期以及可控线程数等功能的线程池。

分类

1、CachedThreadPool:是一个可缓存的无界线程池,如果线程池长度超过处理需要,可灵活回收空线程,若无可回收,则新建线程。

/**
 * 可缓存无界线程池测试
 * 当线程池中的线程空闲时间超过60s则会自动回收该线程,核心线程数为0
 * 当任务超过线程池的线程数则创建新线程。线程池的大小上限为Integer.MAX_VALUE,
 * 可看做是无限大。
 */
@Test
public void cacheThreadPoolTest() {
    // 创建可缓存的无界线程池,可以指定线程工厂,也可以不指定线程工厂
    ExecutorService executorService = Executors.newCachedThreadPool(new testThreadPoolFactory("cachedThread"));
    for (int i = 0; i < 10; i++) {
        executorService.submit(() -> {
            print("cachedThreadPool");
            System.out.println(Thread.currentThread().getName());
                }
        );
    }
}

2、FixedThreadPool:固定大小的线程池,可控制线程的最大并发数,超出的线程会在LinkedBlockingQueue阻塞队列中等待。

/**
 * 创建固定线程数量的线程池测试
 * 创建一个固定大小的线程池,该方法可指定线程池的固定大小,对于超出的线程会在LinkedBlockingQueue队列中等待
 * 核心线程数可以指定,线程空闲时间为0
 */
@Test
public void fixedThreadPoolTest() {
    ExecutorService executorService = Executors.newFixedThreadPool(5, new testThreadPoolFactory("fixedThreadPool"));
    for (int i = 0; i < 10; i++) {
        executorService.submit(() -> {
                    print("fixedThreadPool");
                    System.out.println(Thread.currentThread().getName());
                }
        );
    }
}

3、ScheduledThreadPool:定时周期执行的线程池,支持定时及周期性任务的执行。

/**
 * 创建定时周期执行的线程池测试
 *
 * schedule(Runnable command, long delay, TimeUnit unit),延迟一定时间后执行Runnable任务;
 * schedule(Callable callable, long delay, TimeUnit unit),延迟一定时间后执行Callable任务;
 * scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit),延迟一定时间后,以间隔period时间的频率周期性地执行任务;
 * scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit),与scheduleAtFixedRate()方法很类似,
 * 但是不同的是scheduleWithFixedDelay()方法的周期时间间隔是以上一个任务执行结束到下一个任务开始执行的间隔,而scheduleAtFixedRate()方法的周期时间间隔是以上一个任务开始执行到下一个任务开始执行的间隔,
 * 也就是这一些任务系列的触发时间都是可预知的。
 * ScheduledExecutorService功能强大,对于定时执行的任务,建议多采用该方法。
 */
@Test
public void scheduleThreadPoolTest() {
    // 创建指定核心线程数,但最大线程数是Integer.MAX_VALUE的可定时执行或周期执行任务的线程池
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5, new testThreadPoolFactory("scheduledThread"));

    // 定时执行一次的任务,延迟1s后执行
    executorService.schedule(new Runnable() {
        @Override
        public void run() {
            print("scheduleThreadPool");
            System.out.println(Thread.currentThread().getName() + ", delay 1s");
        }
    }, 1, TimeUnit.SECONDS);


    // 周期性地执行任务,延迟2s后,每3s一次地周期性执行任务
    executorService.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + ", every 3s");
        }
    }, 2, 3, TimeUnit.SECONDS);


    executorService.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            long start = new Date().getTime();
            System.out.println("scheduleWithFixedDelay 开始执行时间:" +
                    DateFormat.getTimeInstance().format(new Date()));
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long end = new Date().getTime();
            System.out.println("scheduleWithFixedDelay执行花费时间=" + (end - start) / 1000 + "m");
            System.out.println("scheduleWithFixedDelay执行完成时间:"
                    + DateFormat.getTimeInstance().format(new Date()));
            System.out.println("======================================");
        }
    }, 1, 2, TimeUnit.SECONDS);

}

4、SingleThreadExecutor:单线程的线程池,它只有一个线程,用仅有的一个线程来执行任务,保证所有的任务按照指定顺序(FIFO,LIFO,优先级)执行,所有的线程都保存在队列 LinkedBlockingQueue 中,等待唯一的单线程来执行任务。

/**
 * 创建只有一个线程的线程池测试
 * 该方法无参数,所有任务都保存队列LinkedBlockingQueue中,核心线程数为1,线程空闲时间为0
 * 等待唯一的单线程来执行任务,并保证所有任务按照指定顺序(FIFO或优先级)执行
 */
@Test
public void singleThreadPoolTest() {
    // 创建仅有单个线程的线程池
    ExecutorService executorService = Executors.newSingleThreadExecutor(new testThreadPoolFactory("singleThreadPool"));
    for (int i = 0; i < 10; i++) {
        executorService.submit(() -> {
                    print("singleThreadPool");
                    System.out.println(Thread.currentThread().getName());
                }
        );
    }

}

线程池原理

Executors类提供4个静态工厂方法:newCachedThreadPool()、newFixedThreadPool(int)、newSingleThreadExecutor和newScheduledThreadPool(int)。这些方法最终都是通过ThreadPoolExecutor类来完成的,这里强烈建议大家直接使用Executors类提供的便捷的工厂方法,能完成绝大多数的用户场景,当需要更细节地调整配置,需要先了解每一项参数的意义。

ThreadPoolExecutor

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         Executors.defaultThreadFactory(), defaultHandler);
}

创建线程池,在构造一个新的线程池时,必须满足下面的条件:

七大参数

1、corePoolSize(核心线程数)线程池基本大小,必须大于或等于0;
2、maximumPoolSize(最大线程数)线程池最大大小,必须大于或等于1;
3、keepAliveTime(空闲线程存活时间),必须大于或等于0;
4、unit(单元)keepAliveTime的单位。
5、workQueue(任务队列)被提交但尚未执行的队列,不能为空;
6、threadFactory(线程工厂)不能为空,默认为DefaultThreadFactory类
7、handler(拒绝策略),表示当线程队列满了并且工作线程大于等于线程池的最大显示数(maxnumPoolSize)时如何来拒绝请求执行的runnable的策略。

参考:
https://blog.csdn.net/qq_40093255/article/details/116990431

线程池中的七大参数 - 蜡笔没了芯 - 博客园 (cnblogs.com)

posted @ 2023-02-27 10:39  村上春树的叶子  阅读(13)  评论(0编辑  收藏  举报