12.线程池(重点)

为什么使用线程池优势?
    线程池做的工作主要是控制运行的线程的数量,处理过程中将任务放入队列,然后在线程创建后启动这些任务,如果线程数量超过最大数量,超出部分将排队等待。
    其他线程执行完毕,再从队列中取出任务来执行
主要特点:
    1.线程复用
    2.控制最大并发数。
    3.管理线程
优点:
     1.降低资源消耗:通过重复利用创建的线程,降低线程创建和销毁造成的消耗。
    2.提高响应速度:当任务到达时,任务可以不需要等到线程创建就能立即执行
    3.提高线程的可管理性:线程是稀缺资源,如果无限的创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池,可以进行统一的分配,调优和监控。
    
    
线程池重点:
      1.三大方法
     2.七大参数
     3.四种拒绝策略
 
池化技术:
     程序的运行本质是:占用系统的资源!优化资源的使用==>池化技术
     线程池、连接池、内存池、对象池等等 创建销毁十分浪费资源
     池化技术:事先准备好一些资源,有人要用,就拿来用,用完归还

-----------------------------------------------------------------------------------------------------------------------------------------------------------
                                                        Executors
-----------------------------------------------------------------------------------------------------------------------------------------
 
 
 重点1:线程池的常用的三大方法(总共有5种):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 {
                        //重点3:关闭线程池
                        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.MAX_VALUE
        }
        
    2.newFixedThreadPool:固定大小的线程池
        public static ExecutorService newFixedThreadPool(int nThreads) {
            return new ThreadPoolExecutor(nThreads, nThreads,
                                          0L, TimeUnit.MILLISECONDS,
                                          new LinkedBlockingQueue<Runnable>());不指定队列大小,默认是Integer.MAX_VALUE
        }

    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,//阻塞队列:不指定队列大小,默认是Integer.MAX_VALUE
                          ThreadFactory threadFactory,//线程工厂,创建线程的,一般不用动
                          RejectedExecutionHandler handler//拒绝策略
                          ) {
        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;
    }
    
    
实际开发中我们不允许使用Executors去创建,而是通过ThreadPollExcutor的方式去自定义,这样的处理方式可以让写的同学明确线程池的运行规则,
避免资源耗尽的风险
    说明:Executors返回的线程池对象的弊端如下:
        1)FixedThreadPool和SingleThreadPool:
            允许的请求队列长度为Integer.Max_Value,可能会堆积大量的请求,从而导致OOM
        2)CachedThreadPool和ScheduledThreadPool
            允许创建的最大线程数量为Integer.Max_value,可能会创建大量线程,从而导致OOM


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
问题:
        为什么单个线程的线程池/固定大小的线程池,因为默认拒绝策略(AbortPolicy,超出抛异常)很多请求并发来并没有抛异常呢,因为其构造方法中的队列:
         public static ExecutorService newSingleThreadExecutor() {
            return new FinalizableDelegatedExecutorService
                (new ThreadPoolExecutor(1, 1,
                                        0L, TimeUnit.MILLISECONDS,
                                        new LinkedBlockingQueue<Runnable>()));不指定队列大小,默认是Integer.MAX_VALUE
        }  
        不传值为int的最大值,即缓存区的大小很大,所以多个线程会进入缓存区等待,并不满足抛异常的条件!(鸡贼!!)
        
            
                    
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()//拒绝策略
    );

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

池的最大的大小如何设置?(调优)
获取cpu核数:Runtime.getRuntime().availableProcessors()
根据业务分为
    1.IO密集型
        1.1:由于IO密集型的任务并不是一直在执行任务,则应该配置尽可能多的线程:如cpu核数*2
        1.2:IO密集型,即该任务需要大量的IO,即大量的阻塞。
            在单线程上运行IO密集型的任务会导致大量的CPU运算能力浪费在等待
            所以在IO密集型任务中使用多线程可以大大的加速程序运行,即使在单核的cpu上,这种加速主要就是利用被浪费掉的阻塞时间
            按照下列配置公式:
                CPU核数/(1-阻塞系数)  阻塞习俗在0.8和0.9间,建议取值0.9
                比如8核cpu:8/(1-0.9)=80个线程数
    2.CPU密集型:几何cpu就设置为几+1,可以保证CPU效率最高
        一般都是代码获取cpu核数
        Runtime.getRuntime().availableProcessors();

 

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