6.线程池(重点)

线程池重点:
     1.三大方法
     2.7大参数
     3.4种拒绝策略
 
 池化技术:
     程序的运行本质是:占用系统的资源!优化资源的使用==>池化技术
     线程池、连接池、内存池、对象池等等 创建销毁十分浪费资源
     池化技术:事先准备好一些资源,有人要用,就拿来用,用完归还
 
 线程池的好处:
     1.降低资源的消耗和浪费
     2.提高响应的速度
     3.方便管理
 线程复用、可以控制并发数、管理线程
 
 
 重点1:线程池的三大方法:Executors可以看做是线程池的一个工具类
     1.单个线程
         ExecutorService threadPool = Executors.newSingleThreadExecutor();
         样例代码如下:
             public class ThreadPool {
                public static void main(String[] args) {
                    重点1:创建单个线程的线程池
                    ExecutorService threadPool = Executors.newSingleThreadExecutor();
                    try {
                        for (int i = 0; i < 10; i++) {
                            final  int temp = i;
                            重点2:通过线程池启动线程:execute(Runnable)
                            threadPool.execute(()->{
                                System.out.println(Thread.currentThread().getName()+":"+temp);
                            });
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        threadPool.shutdown();
                    }
                }
            }
        输出:发现至此只有一个线程在执行方法
            pool-1-thread-1:0
            pool-1-thread-1:1
            pool-1-thread-1:2
            pool-1-thread-1:3
            pool-1-thread-1:4
            ...
    2.创建固定大小的线程池Executors.newFixedThreadPool(5)
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        输出:发现同时最多有5个线程在执行
            pool-1-thread-1:0
            pool-1-thread-2:1
            pool-1-thread-1:5
            pool-1-thread-2:6
            pool-1-thread-1:7
            pool-1-thread-2:8
            pool-1-thread-1:9
            pool-1-thread-3:2
            pool-1-thread-4:3
            pool-1-thread-5:4
            
    3.创建可伸缩的线程池:Executors.newCachedThreadPool()创建一个弹性的线程池,会根据cpu性能去创建对应的多个线程!
        ExecutorService threadPool = Executors.newCachedThreadPool();
        输出:发现同时最多有10个线程同时执行!
            pool-1-thread-1:0
            pool-1-thread-2:1
            pool-1-thread-3:2
            pool-1-thread-4:3
            pool-1-thread-5:4
            pool-1-thread-6:5
            pool-1-thread-7:6
            pool-1-thread-8:7
            pool-1-thread-10:9
            pool-1-thread-9:8

重点2:7大参数

研究上述三大方法的源码:
    1.newSingleThreadExecutor:单个线程的线程池
        public static ExecutorService newSingleThreadExecutor() {
            return new FinalizableDelegatedExecutorService
                (new ThreadPoolExecutor(1, 1,
                                        0L, TimeUnit.MILLISECONDS,
                                        new LinkedBlockingQueue<Runnable>()));//未传入队列大小,则为Integer.maxValue
        }
        
    2.newFixedThreadPool:固定大小的线程池
        public static ExecutorService newFixedThreadPool(int nThreads) {
            return new ThreadPoolExecutor(nThreads, nThreads,
                                          0L, TimeUnit.MILLISECONDS,
                                          new LinkedBlockingQueue<Runnable>());//未传入队列大小,则为Integer.maxValue
        }

    3.newCachedThreadPool:可伸缩的线程池
        public static ExecutorService newCachedThreadPool() {
            return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                          60L, TimeUnit.SECONDS,
                                          new SynchronousQueue<Runnable>());//单个的阻塞队列
        }
发现三个方法底层都调用的是:ThreadPoolExecutor方法
    public ThreadPoolExecutor(
                          int corePoolSize,//核心线程池大小
                          int maximumPoolSize,//最大核心线程池大小:包括核心线程池
                          long keepAliveTime,//超时时间:超时没人调用自动释放
                          TimeUnit unit,//超时时间单位
                          BlockingQueue<Runnable> workQueue,//阻塞队列
                          ThreadFactory threadFactory,//线程工厂,创建线程的,一般不用动
                          RejectedExecutionHandler handler//拒绝clue
                          ) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

最大线程数=核心线程数+右边红色线程数

1.平时只有核心线程里的线程数在工作
2.但当请求增多,阻塞队列等待的已满,则最大线程数开始工作,来应对
3.但当线程数最大了,并且阻塞队列已满,再来请求执行拒绝策略

重点3:自定义线程池已经几种阻塞策略使用

线程池的最大并发量=最大核心线程数+阻塞队列中的数量
1:第一种拒绝策略:AbortPolicy:当请求数大于最大并发数时,抛出异常!
    1.1 并发数小于最大核心线程数:所以此时的情况是:核心线程处理两个请求,剩余三个在阻塞区等待,最大核心线程未启动!
        public class ThreadPool {
            public static void main(String[] args) {
                重点1:创建自定义线程池
                ExecutorService threadPool = new ThreadPoolExecutor(
                        2,//核心线程池数量
                        5,//最大线程池数
                        3,//超时时间,最大线程池里的线程超过时间无人调用时,归还线程资源
                        TimeUnit.SECONDS,//超时时间单位
                        new LinkedBlockingQueue<>(3),//阻塞队列:休息区的数量
                        Executors.defaultThreadFactory(),//默认的线程工厂
                        new ThreadPoolExecutor.AbortPolicy()//拒绝策略
                );
                try {
                    重点2:并发数小于最大核心线程数:所以此时的情况是:核心线程处理两个请求,剩余三个在阻塞区等待,最大核心线程未启动!
                    for (int i = 0; i < 5; i++) {
                        final  int temp = i;
                        threadPool.execute(()->{
                            System.out.println(Thread.currentThread().getName()+":"+temp);
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    threadPool.shutdown();
                }
            }
        }
        输出:发现只有两个线程并发处理请求
            pool-1-thread-1:0
            pool-1-thread-2:1
            pool-1-thread-1:2
            pool-1-thread-2:3
            pool-1-thread-2:4
     1.2 并发数>核心线程数+阻塞线程数,最大核心线程启动,但是阻塞区依旧阻塞3个线程:
         并发6个线程>核心线程数2+阻塞区线程数3,所以此时,最大线程区给出相差的1,此时有三个线程同时处理,剩余的阻塞区等待
             for (int i = 0; i < 6; i++) {
                    final  int temp = i;
                    threadPool.execute(()->{
                        System.out.println(Thread.currentThread().getName()+":"+temp);
                    });
                }
          输出:最大3个线程执行
                pool-1-thread-1:0
                pool-1-thread-2:1
                pool-1-thread-2:2
                pool-1-thread-2:3
                pool-1-thread-2:4
                pool-1-thread-3:5
    1.3 并发数>最大线程数+阻塞线程数
        此时并发数9>最大线程数5+阻塞区3,有一个线程无法访问资源,此时的拒绝策略:AbortPolicy会抛出异常!
            try {
                for (int i = 0; i < 9; i++) {
                    final  int temp = i;
                    threadPool.execute(()->{
                        System.out.println(Thread.currentThread().getName()+":"+temp);
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                threadPool.shutdown();
            }
        输出:
            pool-1-thread-1:0
            pool-1-thread-1:2
            pool-1-thread-1:3
            pool-1-thread-1:4
            pool-1-thread-2:1
            pool-1-thread-3:5
            pool-1-thread-4:6
            pool-1-thread-5:7
            java.util.concurrent.RejectedExecutionException
            
            
2.第二种拒绝策略:CallerRunsPolicy,哪来的回哪去
        ExecutorService threadPool = new ThreadPoolExecutor(
            2,//核心线程池数量
            5,//最大线程池数
            3,//超时时间,最大线程池里的线程超过时间无人调用时,归还线程资源
            TimeUnit.SECONDS,//超时时间单位
            new LinkedBlockingQueue<>(3),//阻塞队列:休息区的数量
            Executors.defaultThreadFactory(),//默认的线程工厂
            new ThreadPoolExecutor.CallerRunsPolicy()//拒绝策略:哪来的回哪去
        );
        try {
            for (int i = 0; i < 9; i++) {
                final  int temp = i;
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+":"+temp);
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    输出:发现超出的线程由调用的main线程执行了!
        pool-1-thread-1:0
        main:8
        pool-1-thread-2:1
        pool-1-thread-1:2
        pool-1-thread-2:3
        pool-1-thread-1:4
        pool-1-thread-4:6
        pool-1-thread-5:7
        pool-1-thread-3:5

3.DiscardPolicy:丢弃任务,不会抛出异常!
    ExecutorService threadPool = new ThreadPoolExecutor(
            2,//核心线程池数量
            5,//最大线程池数
            3,//超时时间,最大线程池里的线程超过时间无人调用时,归还线程资源
            TimeUnit.SECONDS,//超时时间单位
            new LinkedBlockingQueue<>(3),//阻塞队列:休息区的数量
            Executors.defaultThreadFactory(),//默认的线程工厂
            //DiscardPolicy的拒绝策略,丢弃任务,不会抛出异常!
            new ThreadPoolExecutor.DiscardPolicy()//拒绝策略
        );
        try {
            for (int i = 0; i < 9; i++) {
                final  int temp = i;
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+":"+temp);
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    输出:发现5个线程同时执行,多出的那个线程被丢弃!
        pool-1-thread-1:0
        pool-1-thread-4:6
        pool-1-thread-3:5
        pool-1-thread-2:1
        pool-1-thread-4:2
        pool-1-thread-5:7
        pool-1-thread-4:4
        pool-1-thread-1:3

4.DiscardOldestPolicy:队列满了是,尝试和最早的线程竞争,也不会抛出异常!
    ExecutorService threadPool = new ThreadPoolExecutor(
            2,//核心线程池数量
            5,//最大线程池数
            3,//超时时间,最大线程池里的线程超过时间无人调用时,归还线程资源
            TimeUnit.SECONDS,//超时时间单位
            new LinkedBlockingQueue<>(3),//阻塞队列:休息区的数量
            Executors.defaultThreadFactory(),//默认的线程工厂
            new ThreadPoolExecutor.DiscardOldestPolicy()//拒绝策略
    );

线程池的最大的大小如何设置呢

池的最大的大小如何设置?(调优)
    1.IO密集型:判断你程序中什么耗资源的IO线程,让后将最最大值设为其2倍
    2.CPU密集型:几何cpu就设置为几,可以保证CPU效率最高
        一般都是代码获取cpu核数
        Runtime.getRuntime().availableProcessors();

 

posted @ 2022-05-13 21:10  努力的达子  阅读(38)  评论(0编辑  收藏  举报