【Java开发】JUC进阶 04:线程池详解

1 线程池介绍

由于频繁创建销毁线程要调用native方法比较消耗资源,为了保证内核的充分利用,所以引入了线程池的概念。

📌 线程池优点

  • 降低资源消耗

  • 提高响应速度

  • 方便管理

📌 创建线程池

  • 使用Executors创建

  • 使用ThreadPoolExecutor创建

📌 工作流程

2 Executors创建线程池

📌 三大方法

  • ExecutorService threadPool = Executors.newSingleThreadExecutor();--创建单个线程

  • ExecutorService threadPool = Executors.newFixedThreadPool(5);--创建固定大小的线程池

  • ExecutorService threadPool = Executors.newCachedThreadPool();--创建不固定大小的线程池

📌 代码举例

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorsDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
//        ExecutorService threadPool = Executors.newFixedThreadPool(5);
//        ExecutorService threadPool = Executors.newCachedThreadPool();

        try {
            //使用线程池创建线程池
            for (int i = 0; i < 5; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" Ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}

3 ThreadPoolExecutor创建线程池

📌 七大参数说明

  1. corePoolSize:核心线程数量,线程池维护线程的最少数量

  1. maximumPoolSize:线程池维护线程的最大数量

  1. keepAliveTime:线程池除核心线程外的其他线程的最长空闲时间,超过该时间的空闲线程会被销毁

  1. unit:keepAliveTime的单位,TimeUnit中的几个静态属性:NANOSECONDS、MICROSECONDS、MILLISECONDS、SECONDS

  1. workQueue:线程池所使用的任务缓冲队列

  1. threadFactory:线程工厂,用于创建线程,一般用默认的即可

  1. handler:线程池对拒绝任务的处理策略

📢 官方推荐ThreadPoolExecutor创建线程池

阿里代码规范如下:

源码解析👇

//1.newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));
}
//2.newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}
//3.newCachedThreadPool
public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE, //21亿
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

public ThreadPoolExecutor(int corePoolSize, // 核心线程数
                          int maximumPoolSize, // 最大线程数 
                          long keepAliveTime, // 存活时间
                          TimeUnit unit, // 存活时间单位
                          BlockingQueue<Runnable> workQueue, // 阻塞队列
                          ThreadFactory threadFactory, // 线程工厂
                          RejectedExecutionHandler handler //拒绝策略) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         threadFactory, defaultHandler);
}

可以看到Executors创建线程池的三个方法都调用了ThreadPoolExecutor!

4 四大拒绝策略

📌 UML图

📌 要点

  • AbortPolicy:被拒绝了抛出异常

  • CallerRunsPolicy:使用调用者所在线程执行,就是哪里来的回哪里去

  • DiscardOldestPolicy: 当触发拒绝策略,只要线程池没有关闭的话,丢弃阻塞队列 workQueue 中最老的一个任务,并将新任务加入

  • DiscardPolicy:直接丢弃,其他啥都没有

4.1 AbortPolicy

ThreadPoolExecutor 默认AbortPolicy拒绝策略

public class AbortPolicyDemo {
    public static void main(String[] args) {
        //自定义线程池!
        ExecutorService threadPool = new ThreadPoolExecutor(
                3,//核心线程为3
                5,//最大线程数是5,包括核心线程
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),//阻塞队列数
                Executors.defaultThreadFactory());

        try {
            for (int i = 0; i < 10; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" OK");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}

控制台输出:

4.2 CallerRunsPolicy

public class CallerRunsPolicyDemo {
    public static void main(String[] args) {
        //自定义线程池!
        ExecutorService threadPool = new ThreadPoolExecutor(
                3,//核心线程为3
                5,//最大线程数是5,包括核心线程
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),//阻塞队列数
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        try {
            for (int i = 0; i < 10; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" OK");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}

控制台输出:

📢 可以看到,多出来的两个线程回到了主线程,因为这10个线程都是从主线程丢到线程池的。

4.3 DiscardPolicy

它会尝试丢掉任务队列中的最早任务,也不会抛出异常。

public class DiscardOldestPolicyDemo {
    public static void main(String[] args) {
        //自定义线程池!
        ExecutorService threadPool = new ThreadPoolExecutor(
                3,//核心线程为3
                5,//最大线程数是5,包括核心线程
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),//阻塞队列数
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());

        try {
            for (int i = 0; i < 8; i++) {
                threadPool.execute(()->{
                    try {//添加等待时间,防止过早结束
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+" OK");
                });
            }
            //第九个线程,他会尝试插入,替代最老的线程
            threadPool.execute(()->{
                System.out.println(Thread.currentThread().getName()+" OK11");
            });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}

控制台输出:

📢 可以看到总数还是8个线程,而且“0K11”输出了,说明其中一个线程被顶掉了。

4.4 DiscardOldestPolicy

public class DiscardPolicyDemo {
    public static void main(String[] args) {
        //自定义线程池!
        ExecutorService threadPool = new ThreadPoolExecutor(
                3,//核心线程为3
                5,//最大线程数是5,包括核心线程
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),//阻塞队列数
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy());

        try {
            for (int i = 0; i < 8; i++) {
                threadPool.execute(()->{
                    try {//添加等待时间,防止过早结束
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+" OK");
                });
            }
            //第九个线程,他会尝试插入,替代最老的线程
            threadPool.execute(()->{
                System.out.println(Thread.currentThread().getName()+" OK11");
            });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}

控制台输出:

📢 第9个线程被丢掉了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹煜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值