线程池简洁版

Posted 孤音x

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程池简洁版相关的知识,希望对你有一定的参考价值。

一、线程池简洁版

每个人有自己的生活方式,每个人有自己对待感情的态度

1.1 概念

使用线程池的好处

  • 降低资源消耗。通过重复利用已创建的线程降低线程创建和销毁造成的销耗。

  • 提高响应速度。当任务到达时,任务可以不需要等待线程创建就能立即执行。

  • 提高线程的可管理性。线程是稀缺资源,如果无限制的创建,不仅会销耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一的分配,调优和监控。

1.2 线程池的主要处理流程

这几张图先看一下,打个照面,等会读完再回来看下,会有意想不到的收货额

hreadPoolExecutor执行示意图

1.3 线程池的使用

1.3.1 概述

首先,Java中的线程池是通过Executor框架实现的,该框架中用到了ExecutorExecutorsExecutorServiceThreadPoolExecutor这几个类

1.3.2 三种创建方式

//执行长期任务性能好,创建一个线程池,一池有N个固定的线程,有固定线程数的线程
ExecutorService threadPool = Executors.newFixedThreadPool(5); 
//一个任务一个任务的执行,一池一线程
ExecutorService threadPool = Executors.newSingleThreadExecutor(); 
//执行很多短期异步任务,线程池根据需要创建新线程,
//但在先前构建的线程可用时将重用它们。可扩容,遇强则强
ExecutorService threadPool = Executors.newCachedThreadPool(); 

小案例

private static void threadPoolDemo() 
        ExecutorService threadPool = Executors.newFixedThreadPool(5); //一池 5 个线程
//        ExecutorService threadPool = Executors.newSingleThreadExecutor(); //
//        ExecutorService threadPool = Executors.newCachedThreadPool();
        // 池化技术使用后需要释放
        try 
            // 模拟 10个顾客办理业务,目前池子有 5个工作线程提高服务
            for (int i = 1; i <= 10; i++) 
                try  TimeUnit.SECONDS.sleep(1); catch (InterruptedException e) e.printStackTrace(); 
                threadPool.execute(() -> 
                    System.out.println(Thread.currentThread().getName() + "\\t 办理业务");
                );
                // try  TimeUnit.SECONDS.sleep(4); catch (InterruptedException e) e.printStackTrace(); 
            
        catch (Exception e)
            e.printStackTrace();
        finally 
            threadPool.shutdown();
        
    

1.3.3 使用 ThreadPoolExecutor创建

先放代码,现在看不懂先看下面的

public static void main(String[] args) 
//        threadPoolDemo();
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

        try 
            for(int i=1;i<=8;i++)
//                TimeUnit.SECONDS.sleep(1);
                threadPool.execute(()->
                    System.out.println(Thread.currentThread().getName()+"\\t 办理业务");
                );
            
        catch (Exception e)
            e.printStackTrace();
        finally 
            threadPool.shutdown();
        
    

1.4 线程池的7种参数

我们查阅底层源码可知ThreadPoolExecutor的构造方法有七个参数,这个参数是按照ThreadPoolExecutor里面的参数顺序来写的

参数名解释
corePoolSize线程池中的常驻核心线程数
maximumPoolSize线程池中能够容纳同时执行的最大线程数,此值必须大于等于1
keepAliveTime多余的空闲线程的存活时间
当前池中线程数量超过corePoolSize时,当空闲时间
达到keepAliveTime时,多余线程会被销毁直到
只剩下corePoolSize个线程为止
unitkeepAliveTime的单位
workQueue任务队列,被提交但未被执行的任务
threadFactory表示生成线程池中工作线程的线程工厂,用于创建线程,一般默认的即可
handler拒绝策略,表示当队列满了,并且工作线程大于等于线程池的最大线程数(maximumPoolSize)时如何来拒绝请求执行的runnable的策略

1.5 用哪一个线程池

前面1.3中说了有种创建方式,那么我们在实际开发中,用哪一个比较好呢?

答案是:都不用

我们只使用自定义的,原因是

图中说的方式正是我们上面1.3.3中代码

以上是关于线程池简洁版的主要内容,如果未能解决你的问题,请参考以下文章

线程池简洁版

线程池简洁版

内存池线程池连接池,这些池子里面放的都是什么?

进程池,线程池,协程

使用和理解线程池

线程池实现原理及案列