线程池的工作时控制运行的线程数量,处理过程中将任务放入队列,然后在线程创建后启动这些任务,如果线程数量超过了最大数量,超出数量的线程排队等候,等待其他线程执行完成,再从队列中取出任务来执行。
特点:
线程复用,控制最大并发数,管理线程。
好处:
简单的架构图:
我们使用的其实就是ThreadPoolExecutor.
阿里巴巴开发规范手册:
【强制】线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。
说明:Executors 返回的线程池对象的弊端如下:
1)FixedThreadPool 和 SingleThreadPool:
允许的请求队列长度为 Integer.MAX_VALUE,可能会堆积大量的请求,从而导致 OOM。
2)CachedThreadPool:
允许的创建线程数量为 Integer.MAX_VALUE,可能会创建大量的线程,从而导致 OOM。
首先使用工具类(Executors)来创建线程池:
FixedThreadPool被称为可重用固定线程数的线程池,执行长期任务性能好,创建一个线程池,一池有N个固定的线程,有固定的线程数的线程池。
public class demo1 { public static void main(String[] args) { //使用Executors工具类来创建newFixedThreadPool线程池 ExecutorService executor = Executors.newFixedThreadPool(3); try{ for (int i = 0; i < 10; i++) { executor.execute(()->{ System.out.println(Thread.currentThread().getName()+"任务执行"); }); } } finally { executor.shutdown(); } } }
SingleThreadExecutor是使用单个worker线程的Executor,省去了创建线程和销毁线程时资源消耗。
public class demo1 { public static void main(String[] args) { ExecutorService executor = Executors.newSingleThreadExecutor(); try{ for (int i = 0; i < 10; i++) { executor.execute(()->{ System.out.println(Thread.currentThread().getName()+"\t任务执行"); }); } } finally { executor.shutdown(); } } }
CachedThreadPool是一个会根据需要创建新线程的线程池。可扩容的线程池。
public class demo1 { public static void main(String[] args) { //使用Executors工具类来创建newFixedThreadPool线程池 //ExecutorService executor = Executors.newFixedThreadPool(3); //一个线程池就一个线程 //ExecutorService executor = Executors.newSingleThreadExecutor(); ExecutorService executor = Executors.newCachedThreadPool(); try{ for (int i = 0; i < 10; i++) { executor.execute(()->{ System.out.println(Thread.currentThread().getName()+"\t任务执行"); }); } } finally { executor.shutdown(); } } }
pool-1-thread-2 任务执行 pool-1-thread-1 任务执行 pool-1-thread-4 任务执行 pool-1-thread-3 任务执行 pool-1-thread-5 任务执行 pool-1-thread-6 任务执行 pool-1-thread-7 任务执行 pool-1-thread-8 任务执行 pool-1-thread-9 任务执行 pool-1-thread-10 任务执行
当设置延时操作来模仿耗时的代码时使用线程池的过程。
package com.JucPool; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * 基础认识以及线程池的三大方法 */ public class demo1 { public static void main(String[] args) { ExecutorService executor = Executors.newCachedThreadPool(); try{ for (int i = 0; i < 10; i++) { //暂停几秒 try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } executor.execute(()->{ System.out.println(Thread.currentThread().getName()+"\t任务执行"); }); } } finally { executor.shutdown(); } } }
pool-1-thread-1 任务执行 pool-1-thread-1 任务执行 pool-1-thread-1 任务执行 pool-1-thread-1 任务执行 pool-1-thread-1 任务执行 pool-1-thread-1 任务执行 pool-1-thread-1 任务执行 pool-1-thread-1 任务执行 pool-1-thread-1 任务执行 pool-1-thread-1 任务执行 Process finished with exit code 0
这时就降为SingleThreadExecutor线程池。
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
通过对比发现,底层都是通过ThreadPoolExecutor来实现,只是参数的不同,传递的参数与阻塞队列也有关系。
参数:(非常重要)
在ThreadPoolExecutor中还存在两个参数:
上述是线程池中的七大参数总结,很重要,关于什么情况下走到拒绝策略,后面会分析线程池的执行流程。