Java四种常用线程池的详细介绍
一.线程池简介
1.线程池的概念:
线程池就是首先创建一些线程,它们的集合称为线程池。使用线程池可以很好地提高性能,线程池在系统启动时即创建大量空闲的线程,程序将一个任务传给线程池,线程池就会启动一条线程来执行这个任务,执行结束以后,该线程并不会死亡,而是再次返回线程池中成为空闲状态,等待执行下一个任务。
2.线程池的工作机制
2.1在线程池的编程模式下,任务是提交给整个线程池,而不是直接提交给某个线程,线程池在拿到任务后,就在内部寻找是否有空闲的线程,如果有,则将任务交给某个空闲的线程。
2.2一个线程同时只能执行一个任务,但可以同时向一个线程池提交多个任务。
3.使用线程池的原因:
多线程运行时间,系统不断的启动和关闭新线程,成本非常高,会过渡消耗系统资源,以及过渡切换线程的危险,从而可能导致系统资源的崩溃。这时,线程池就是最好的选择了。
二.四种常见的线程池详解
1.线程池的返回值ExecutorService简介:
ExecutorService是Java提供的用于管理线程池的类。该类的两个作用:控制线程数量和重用线程
2.具体的4种常用的线程池实现如下:(返回值都是ExecutorService)
2.1Executors.newCacheThreadPool():可缓存线程池,先查看池中有没有以前建立的线程,如果有,就直接使用。如果没有,就建一个新的线程加入池中,缓存型池子通常用于执行一些生存期很短的异步型任务
示例代码:
packagecom.study.test;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
publicclassThreadPoolExecutorTest{
publicstaticvoidmain(String[]args){
//创建一个可缓存线程池
ExecutorServicecachedThreadPool=Executors.newCachedThreadPool();
for(inti=0;i<10;i++){
try{
//sleep可明显看到使用的是线程池里面以前的线程,没有创建新的线程
Thread.sleep(1000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
cachedThreadPool.execute(newRunnable(){
publicvoidrun(){
//打印正在执行的缓存线程信息
System.out.println(Thread.currentThread().getName()+"正在被执行");
}
});
}
}
}
输出结果:
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正在被执行
线程池为无限大,当执行当前任务时上一个任务已经完成,会复用执行上一个任务的线程,而不用每次新建线程
2.2 Executors.newFixedThreadPool(intn):创建一个可重用固定个数的线程池,以共享的无界队列方式来运行这些线程。
示例代码:
packagecom.study.test;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
publicclassThreadPoolExecutorTest{
publicstaticvoidmain(String[]args){
//创建一个可重用固定个数的线程池
ExecutorServicefixedThreadPool=Executors.newFixedThreadPool(3);
for(inti=0;i<10;i++){
fixedThreadPool.execute(newRunnable(){
publicvoidrun(){
try{
//打印正在执行的缓存线程信息
System.out.println(Thread.currentThread().getName()+"正在被执行");
Thread.sleep(2000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
});
}
}
}
输出结果:
pool-1-thread-1正在被执行
pool-1-thread-2正在被执行
pool-1-thread-3正在被执行
pool-1-thread-1正在被执行
pool-1-thread-2正在被执行
pool-1-thread-3正在被执行
pool-1-thread-1正在被执行
pool-1-thread-2正在被执行
pool-1-thread-3正在被执行
pool-1-thread-1正在被执行
因为线程池大小为3,每个任务输出打印结果后sleep2秒,所以每两秒打印3个结果。
定长线程池的大小最好根据系统资源进行设置。如Runtime.getRuntime().availableProcessors()
2.3 Executors.newScheduledThreadPool(intn):创建一个定长线程池,支持定时及周期性任务执行
延迟执行示例代码:
packagecom.study.test;
importjava.util.concurrent.Executors;
importjava.util.concurrent.ScheduledExecutorService;
importjava.util.concurrent.TimeUnit;
publicclassThreadPoolExecutorTest{
publicstaticvoidmain(String[]args){
//创建一个定长线程池,支持定时及周期性任务执行——延迟执行
ScheduledExecutorServicescheduledThreadPool=Executors.newScheduledThreadPool(5);
//延迟1秒执行
scheduledThreadPool.schedule(newRunnable(){
publicvoidrun(){
System.out.println("延迟1秒执行");
}
},1,TimeUnit.SECONDS);
}
}
输出结果:
延迟1秒执行
定期执行示例代码:
packagecom.study.test;
importjava.util.concurrent.Executors;
importjava.util.concurrent.ScheduledExecutorService;
importjava.util.concurrent.TimeUnit;
publicclassThreadPoolExecutorTest{
publicstaticvoidmain(String[]args){
//创建一个定长线程池,支持定时及周期性任务执行——定期执行
ScheduledExecutorServicescheduledThreadPool=Executors.newScheduledThreadPool(5);
//延迟1秒后每3秒执行一次
scheduledThreadPool.scheduleAtFixedRate(newRunnable(){
publicvoidrun(){
System.out.println("延迟1秒后每3秒执行一次");
}
},1,3,TimeUnit.SECONDS);
}
}
输出结果:
延迟1秒后每3秒执行一次
延迟1秒后每3秒执行一次
.............
2.4 Executors.newSingleThreadExecutor():创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO,LIFO,优先级)执行。
示例代码:
packagecom.study.test;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
publicclassTestThreadPoolExecutor{
publicstaticvoidmain(String[]args){
//创建一个单线程化的线程池
ExecutorServicesingleThreadExecutor=Executors.newSingleThreadExecutor();
for(inti=0;i<10;i++){
finalintindex=i;
singleThreadExecutor.execute(newRunnable(){
publicvoidrun(){
try{
//结果依次输出,相当于顺序执行各个任务
System.out.println(Thread.currentThread().getName()+"正在被执行,打印的值是:"+index);
Thread.sleep(1000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
});
}
}
}
输出结果:
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
pool-1-thread-1正在被执行,打印的值是:5
pool-1-thread-1正在被执行,打印的值是:6
pool-1-thread-1正在被执行,打印的值是:7
pool-1-thread-1正在被执行,打印的值是:8
pool-1-thread-1正在被执行,打印的值是:9
三.缓冲队列BlockingQueue和自定义线程池ThreadPoolExecutor
1.缓冲队列BlockingQueue简介:
BlockingQueue是双缓冲队列。BlockingQueue内部使用两条队列,允许两个线程同时向队列一个存储,一个取出操作。在保证并发安全的同时,提高了队列的存取效率。
2.常用的几种BlockingQueue:
- ArrayBlockingQueue(inti):规定大小的BlockingQueue,其构造必须指定大小。其所含的对象是FIFO顺序排序的。
- LinkedBlockingQueue()或者(inti):大小不固定的BlockingQueue,若其构造时指定大小,生成的BlockingQueue有大小限制,不指定大小,其大小有Integer.MAX_VALUE来决定。其所含的对象是FIFO顺序排序的。
- PriorityBlockingQueue()或者(inti):类似于LinkedBlockingQueue,但是其所含对象的排序不是FIFO,而是依据对象的自然顺序或者构造函数的Comparator决定。
- SynchronizedQueue():特殊的BlockingQueue,对其的操作必须是放和取交替完成。
3.自定义线程池(ThreadPoolExecutor和BlockingQueue连用):
自定义线程池,可以用ThreadPoolExecutor类创建,它有多个构造方法来创建线程池。
常见的构造函数:ThreadPoolExecutor(intcorePoolSize,intmaximumPoolSize,longkeepAliveTime,TimeUnitunit,BlockingQueue
示例代码:
packagecom.study.test;
importjava.util.concurrent.ArrayBlockingQueue;
importjava.util.concurrent.BlockingQueue;
importjava.util.concurrent.ThreadPoolExecutor;
importjava.util.concurrent.TimeUnit;
classTempThreadimplementsRunnable{
@Override
publicvoidrun(){
//打印正在执行的缓存线程信息
System.out.println(Thread.currentThread().getName()+"正在被执行");
try{
//sleep一秒保证3个任务在分别在3个线程上执行
Thread.sleep(1000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
publicclassTestThreadPoolExecutor{
publicstaticvoidmain(String[]args){
//创建数组型缓冲等待队列
BlockingQueuebq=newArrayBlockingQueue(10);
//ThreadPoolExecutor:创建自定义线程池,池中保存的线程数为3,允许最大的线程数为6
ThreadPoolExecutortpe=newThreadPoolExecutor(3,6,50,TimeUnit.MILLISECONDS,bq);
//创建3个任务
Runnablet1=newTempThread();
Runnablet2=newTempThread();
Runnablet3=newTempThread();
//Runnablet4=newTempThread();
//Runnablet5=newTempThread();
//Runnablet6=newTempThread();
//3个任务在分别在3个线程上执行
tpe.execute(t1);
tpe.execute(t2);
tpe.execute(t3);
//tpe.execute(t4);
//tpe.execute(t5);
//tpe.execute(t6);
//关闭自定义线程池
tpe.shutdown();
}
}
输出结果:
pool-1-thread-1正在被执行
pool-1-thread-2正在被执行
pool-1-thread-3正在被执行
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接