
1. 为什么要用线程池?有什么优势?
例子:
10年前单核CPU电脑,假的多线程,像马戏团小丑玩多个球,CPU需要来回切换。现在是多核电脑,多个线程各自跑在独立的CPU上,不用切换效率高。
线程池的优势:
线程池做的工作只要是控制运行的线程数量,处理过程中将任务放入队列,然后在线程创建后启动这些任务,如果线程数量超过了最大数量,超出数量的线程排队等候,等其他线程执行完毕,再从队列中取出任务来执行。
它的主要特点为:
线程复用;控制最大并发数;管理线程。
第一:降低资源消耗。通过重复利用已创建的线程降低线程创建和销毁造成的销耗。
第二:提高响应速度。当任务到达时,任务可以不需要等待线程创建就能立即执行。
第三:提高线程的可管理性。线程是稀缺资源,如果无限制的创建,不仅会销耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一的分配,调优和监控。
二级目录
三级目录
2.线程池如何使用:
线程池的三大方法:
1. Executors.newFixedThreadPool(5)
package JUCTest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* @author fly
*
*/
public class MyThreadPoolDemo26 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(5);//五个工作线程的线程池(固定的)
try {
//模拟有10个顾客来银行办理业务,目前银行里面有5个工作人员提供服务
for(int i = 1;i <= 10;i++) {
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" 办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
}finally {
threadPool.shutdown();//关闭线程池
}
}
}
************************
pool-1-thread-3 办理业务
pool-1-thread-1 办理业务
pool-1-thread-4 办理业务
pool-1-thread-5 办理业务
pool-1-thread-2 办理业务
pool-1-thread-5 办理业务
pool-1-thread-4 办理业务
pool-1-thread-1 办理业务
pool-1-thread-3 办理业务
pool-1-thread-2 办理业务
2.Executors.newSingleThreadExecutor();//只有单个工作线程的线程池
ExecutorService threadPool = Executors.newSingleThreadExecutor();//只有单个工作线程的线程池
try {
//模拟有10个顾客来银行办理业务,目前银行里面有5个工作人员提供服务
for(int i = 1;i <= 10;i++) {
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" 办理业务");
});
TimeUnit.MILLISECONDS.sleep(100L);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
threadPool.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 办理业务
3.Executors.newCachedThreadPool()://线程数课扩容的线程池(带缓存的线程池)
ExecutorService threadPool = Executors.newCachedThreadPool();//线程数课扩容的线程池(带缓存的线程池)
try {
//模拟有10个顾客来银行办理业务,目前银行里面有5个工作人员提供服务
for(int i = 1;i <= 10;i++) {
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" 办理业务");
});
TimeUnit.MILLISECONDS.sleep(100L);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
threadPool.shutdown();//关闭线程池
}
3.线程池的七大重要参数:
1、corePoolSize:线程池中的常驻核心线程数;
2、maximumPoolSize:线程池中能够容纳同时执行的最大线程数,此值必须大于等于1;
3、keepAliveTime:多余的空闲线程的存活时间当前池中线程数量超过corePoolSize时,当空闲时间达到keepAliveTime时,多余线程会被销毁直到只剩下corePoolSize个线程为止;
4、unit: keepAliveTime的单位;
5、workQueue:任务队列,被提交但尚未被执行的任务;
6、threadFactory:表示生成线程池中工作线程的线程工厂,用于创建线程,一般默认的即可;
7、handler:拒绝策略,表示当队列满了,并且工作线程大于等于线程池的最大线程数(maximumPoolSize)时如何来拒绝请求执行的runnable的策略;
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
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;
}
4.线程池底层工作原理:
三大方法的底层:
Executors.newFixedThreadPool(5):
Executors.newSingleThreadExecutor():
Executors.newCachedThreadPool():
本质
三个生成线程池的方法底层调用的都是同一个方法:new ThreadPoolExecutor();只是传的参数不同。
原理:
正在执行的线程数等于CorePooleSize,且阻塞队列也满员了,此时如果还有任务进来,name就会开始扩容,增加CorePooleSize的大小,扩容到CorePooleSize达到maxiumPoolSize,之后线程池不再开启新的线程,而是采用拒绝策略,不再允许新的任务使用线程池
扩容:
1、在创建了线程池后,开始等待请求。
2、当调用execute()方法添加一个请求任务时,线程池会做出如下判断:
2.1如果正在运行的线程数量小于corePoolSize,那么马上创建线程运行这个任务;
2.2如果正在运行的线程数量大于或等于corePoolSize,那么将这个任务放入队列;
2.3如果这个时候队列满了且正在运行的线程数量还小于maximumPoo1Size,那么还是 要创建非核心线程立刻运行这个任务;
2.4如果队列满了且正在运行的线程数量大于或等于maximumPoolSize,那么线程池会 启动饱和拒绝策略来执行。
3、当一个线程完成任务时,它会从队列中取下一个任务来执行。
4、当一个线程无事可做超过一定的时间(keepAliveTime)时,线程会判断:
如果当前运行的线程数大于corePoolSize,那么这个线程就被停掉。所以线程池的所有任务完成后,它最终会收缩到corePoolSize的大小。
5.如何手撕线程池?以及定义参数?
如何创建线程池:
最大执行任务的数量:maximumPoolSize+阻塞队列的长度
超过则抛出异常:拒绝执行异常:java.util.concurrent.RejectedExecutionException
代码:
public class MyThreadPoolDemo26 {
public static void main(String[] args) {
System.out.println("CPU处理器数(内核):"+Runtime.getRuntime().availableProcessors());
ExecutorService threadPool = new ThreadPoolExecutor(2, //核心数
5, //最大线程数 CPU密集型电脑 一般设置值为:比电脑核数多1或者2 I/O密集型电脑:一般设置为:1除以核数除以阻塞系数
2L, //扩容线程的最大活跃时间(等待时间)
TimeUnit.SECONDS, //时间格式
new LinkedBlockingDeque<Runnable>(3),//阻塞队列//如果不指名队列大小,则默认Integer.MAXVALUE
Executors.defaultThreadFactory(),//线程工厂
new ThreadPoolExecutor.DiscardOldestPolicy());//拒绝策略
try {
//模拟有10个顾客来银行办理业务,目前银行里面有5个工作人员提供服务
for(int i = 1;i <= 10;i++) {
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" 办理业务");
});
// TimeUnit.MILLISECONDS.sleep(100L);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
threadPool.shutdown();//关闭线程池
}
}
}
6.拒绝策略:
1.AbortPolicy():默认策略。
2.CallerRunsPolicy():
3.DiscardPolicy():
十个任务,抛弃了两个,只执行了八个(最大任务量)
4.DiscardOldestPolicy():
抛弃等待队列中等待时间最长的任务
代码:
public class MyThreadPoolDemo26 {
public static void main(String[] args) {
System.out.println("CPU处理器数(内核):"+Runtime.getRuntime().availableProcessors());
ExecutorService threadPool = new ThreadPoolExecutor(2, //核心数
5, //最大线程数 CPU密集型电脑 一般设置值为:比电脑核数多1或者2 I/O密集型电脑:一般设置为:1除以核数除以阻塞系数
2L, //扩容线程的最大活跃时间(等待时间)
TimeUnit.SECONDS, //时间格式
new LinkedBlockingDeque<Runnable>(3),//阻塞队列//如果不指名队列大小,则默认Integer.MAXVALUE
Executors.defaultThreadFactory(),//线程工厂
new ThreadPoolExecutor.DiscardOldestPolicy());//拒绝策略(可以修改)
//new ThreadPoolExecutor.AbortPolicy();
//new ThreadPoolExecutor.CallerRunsPolicy();
//new ThreadPoolExecutor.DiscardPolicy();
try {
//模拟有10个顾客来银行办理业务,目前银行里面有5个工作人员提供服务
for(int i = 1;i <= 10;i++) {
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" 办理业务");
});
// TimeUnit.MILLISECONDS.sleep(100L);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
threadPool.shutdown();//关闭线程池
}
}
}