您好,欢迎访问一九零五行业门户网

Java线程池是什么?Java线程池的详细讲解

本篇文章给大家带来的内容是关于java线程池是什么?java线程池的详细讲解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1、什么是线程池: java.util.concurrent.executors提供了一个 java.util.concurrent.executor接口的实现用于创建线程池
多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力。    
假设一个服务器完成一项任务所需时间为:t1 创建线程时间,t2 在线程中执行任务的时间,t3 销毁线程时间。
如果:t1 + t3 远大于 t2,则可以采用线程池,以提高服务器性能。
一个线程池包括以下四个基本组成部分: 
1、线程池管理器(threadpool):用于创建并管理线程池,包括 创建线程池,销毁线程池,添加新任务;
2、工作线程(poolworker):线程池中线程,在没有任务时处于等待状态,可以循环的执行任务;
3、任务接口(task):每个任务必须实现的接口,以供工作线程调度任务的执行,它主要规定了任务的入口,任务执行完后的收尾工作,任务的执行状态等;
4、任务队列(taskqueue):用于存放没有处理的任务。提供一种缓冲机制。
 线程池技术正是关注如何缩短或调整t1,t3时间的技术,从而提高服务器程序性能的。它把t1,t3分别安排在服务器程序的启动和结束的时间段或者一些空闲的时间段,这样在服务器程序处理客户请求时,不会有t1,t3的开销了。
线程池不仅调整t1,t3产生的时间段,而且它还显著减少了创建线程的数目,看一个例子:
假设一个服务器一天要处理50000个请求,并且每个请求需要一个单独的线程完成。在线程池中,线程数一般是固定的,所以产生线程总数不会超过线程池中线程的数目,而如果服务器不利用线程池来处理这些请求则线程总数为50000。一般线程池大小是远小于50000。所以利用线程池的服务器程序不会为了创建50000而在处理请求时浪费时间,从而提高效率。
2.常见线程池①newsinglethreadexecutor
单个线程的线程池,即线程池中每次只有一个线程工作,单线程串行执行任务
②newfixedthreadexecutor(n)
固定数量的线程池,没提交一个任务就是一个线程,直到达到线程池的最大数量,然后后面进入等待队列直到前面的任务完成才继续执行
③newcachethreadexecutor(推荐使用)
可缓存线程池,当线程池大小超过了处理任务所需的线程,那么就会回收部分空闲(一般是60秒无执行)的线程,当有任务来时,又智能的添加新线程来执行。
④newschedulethreadexecutor
大小无限制的线程池,支持定时和周期性的执行线程
  java提供的线程池更加强大,相信理解线程池的工作原理,看类库中的线程池就不会感到陌生了。
文章2:
java线程池使用说明
一简介线程的使用在java中占有极其重要的地位,在jdk1.4极其之前的jdk版本中,关于线程池的使用是极其简陋的。在jdk1.5之后这一情况有了很大的改观。jdk1.5之后加入了java.util.concurrent包,这个包中主要介绍java中线程以及线程池的使用。为我们在开发中处理线程的问题提供了非常大的帮助。
二:线程池线程池的作用:
线程池作用就是限制系统中执行线程的数量。
     根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果;少了浪费了系统资源,多了造成系统拥挤效率不高。用线程池控制线程数量,其他线程排队等候。一个任务执行完毕,再从队列的中取最前面的任务开始执行。若队列中没有等待进程,线程池的这一资源处于等待。当一个新任务需要运行时,如果线程池中有等待的工作线程,就可以开始运行了;否则进入等待队列。
为什么要用线程池:
1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务。
2.可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为消耗过多的内存,而把服务器累趴下(每个线程需要大约1mb内存,线程开的越多,消耗的内存也就越大,最后死机)。
java里面线程池的顶级接口是executor,但是严格意义上讲executor并不是一个线程池,而只是一个执行线程的工具。真正的线程池接口是executorservice。
比较重要的几个类:
分类作用
executorservice 真正的线程池接口。
scheduledexecutorservice 能和timer/timertask类似,解决那些需要任务重复执行的问题。
threadpoolexecutor executorservice的默认实现。
scheduledthreadpoolexecutor 继承threadpoolexecutor的scheduledexecutorservice接口实现,周期性任务调度的类实现。
要配置一个线程池是比较复杂的,尤其是对于线程池的原理不是很清楚的情况下,很有可能配置的线程池不是较优的,因此在executors类里面提供了一些静态工厂,生成一些常用的线程池。
1. newsinglethreadexecutor
创建一个单线程的线程池。这个线程池只有一个线程在工作,也就是相当于单线程串行执行所有任务。如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它。此线程池保证所有任务的执行顺序按照任务的提交顺序执行。
2.newfixedthreadpool
创建固定大小的线程池。每次提交一个任务就创建一个线程,直到线程达到线程池的最大大小。线程池的大小一旦达到最大值就会保持不变,如果某个线程因为执行异常而结束,那么线程池会补充一个新线程。
3. newcachedthreadpool
创建一个可缓存的线程池。如果线程池的大小超过了处理任务所需要的线程,
那么就会回收部分空闲(60秒不执行任务)的线程,当任务数增加时,此线程池又可以智能的添加新线程来处理任务。此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说jvm)能够创建的最大线程大小。
4.newscheduledthreadpool
创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求。
实例
1:newsinglethreadexecutor
package com.thread; /* *   *通过实现runnable接口,实现多线程 * runnable类是有run()方法的; * 但是没有start方法 * 参考: * http://blog.csdn.net/qq_31753145/article/details/50899119 * */public class mythread extends thread {     @override public void run() { // todo auto-generated method stub // super.run();    system.out.println(thread.currentthread().getname()+正在执行....);    } }
package com.thread; import java.util.concurrent.executorservice; import java.util.concurrent.executors; /*  * 通过实现runnable接口,实现多线程 * runnable类是有run()方法的; * 但是没有start方法 * 参考: * http://blog.csdn.net/qq_31753145/article/details/50899119 * */public class singlethreadexecutortest{ public static void main(string[] args) { // todo auto-generated method stub //创建一个可重用固定线程数的线程池        executorservice pool=executors.newsinglethreadexecutor(); //创建实现了runnable接口对象,thread对象当然也实现了runnable接口; thread t1=new mythread();        thread t2=new mythread();        thread t3=new mythread();        thread t4=new mythread();        thread t5=new mythread(); //将线程放到池中执行; pool.execute(t1);        pool.execute(t2);        pool.execute(t3);        pool.execute(t4);        pool.execute(t5); //关闭线程池 pool.shutdown();    }}
结果:
pool-1-thread-1正在执行....pool-1-thread-1正在执行....pool-1-thread-1正在执行....pool-1-thread-1正在执行....pool-1-thread-1正在执行....
2newfixedthreadpool
 package com.thread; import java.util.concurrent.executorservice; import java.util.concurrent.executors; /* * 通过实现runnable接口,实现多线程 * runnable类是有run()方法的; * 但是没有start方法 * 参考: * http://blog.csdn.net/qq_31753145/article/details/50899119 * */public class fixedthreadexecutortest{ public static void main(string[] args) { // todo auto-generated method stub //创建一个可重用固定线程数的线程池        executorservice pool=executors.newfixedthreadpool(2); //创建实现了runnable接口对象,thread对象当然也实现了runnable接口; thread t1=new mythread();        thread t2=new mythread();        thread t3=new mythread();        thread t4=new mythread();        thread t5=new mythread(); //将线程放到池中执行; pool.execute(t1);        pool.execute(t2);        pool.execute(t3);        pool.execute(t4);        pool.execute(t5); //关闭线程池 pool.shutdown();    }}
结果:
pool-1-thread-1正在执行....pool-1-thread-1正在执行....pool-1-thread-1正在执行....pool-1-thread-1正在执行....pool-1-thread-2正在执行....
3、newcachedthreadpool
package com.thread; import java.util.concurrent.executorservice; import java.util.concurrent.executors; /*  * 通过实现runnable接口,实现多线程 * runnable类是有run()方法的; * 但是没有start方法 * 参考: * http://blog.csdn.net/qq_31753145/article/details/50899119 * */public class cachedthreadexecutortest{ public static void main(string[] args) { // todo auto-generated method stub //创建一个可重用固定线程数的线程池        executorservice pool=executors.newcachedthreadpool(); //创建实现了runnable接口对象,thread对象当然也实现了runnable接口; thread t1=new mythread();        thread t2=new mythread();        thread t3=new mythread();        thread t4=new mythread();        thread t5=new mythread(); //将线程放到池中执行; pool.execute(t1);        pool.execute(t2);        pool.execute(t3);        pool.execute(t4);        pool.execute(t5); //关闭线程池 pool.shutdown();    }}
结果:
pool-1-thread-2正在执行....pool-1-thread-1正在执行....pool-1-thread-3正在执行....pool-1-thread-4正在执行....pool-1-thread-5正在执行....
4、newscheduledthreadpool
package com.thread; import java.util.concurrent.scheduledthreadpoolexecutor; import java.util.concurrent.timeunit; /* * 通过实现runnable接口,实现多线程 * runnable类是有run()方法的; * 但是没有start方法 * 参考: * http://blog.csdn.net/qq_31753145/article/details/50899119 * */public class scheduledthreadexecutortest{ public static void main(string[] args) { // todo auto-generated method stub scheduledthreadpoolexecutor exec =new scheduledthreadpoolexecutor(1);       exec.scheduleatfixedrate(new runnable(){//每隔一段时间就触发异常 @override public void run() { // todo auto-generated method stub //throw new runtimeexception();            system.out.println(===================);        }}, 1000, 5000, timeunit.milliseconds);         exec.scheduleatfixedrate(new runnable(){//每隔一段时间打印系统时间,证明两者是互不影响的 @override public void run() { // todo auto-generated method stub system.out.println(system.nanotime());        }}, 1000, 2000, timeunit.milliseconds);    }}
结果:
===================231193188574912312131907184123123319007891===================2312531817693723127318190359===================231293181761482313131834431223133318465896===================23135319645812
三:threadpoolexecutor详解threadpoolexecutor的完整构造方法的签名是:threadpoolexecutor(int corepoolsize, int maximumpoolsize, long keepalivetime, timeunit unit, blockingqueue<runnable> workqueue, threadfactory threadfactory, rejectedexecutionhandler handler) .
corepoolsize - 池中所保存的线程数,包括空闲线程。
maximumpoolsize-池中允许的最大线程数。
keepalivetime - 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。
unit - keepalivetime 参数的时间单位。
workqueue - 执行前用于保持任务的队列。此队列仅保持由 execute方法提交的 runnable任务。
threadfactory - 执行程序创建新线程时使用的工厂。
handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序。
threadpoolexecutor是executors类的底层实现。
在jdk帮助文档中,有如此一段话:
“强烈建议程序员使用较为方便的executors工厂方法executors.newcachedthreadpool()(无界线程池,可以进行自动线程回收)、executors.newfixedthreadpool(int)(固定大小线程池)executors.newsinglethreadexecutor()(单个后台线程)
它们均为大多数使用场景预定义了设置。”
下面介绍一下几个类的源码:
executorservice  newfixedthreadpool (int nthreads):固定大小线程池。
可以看到,corepoolsize和maximumpoolsize的大小是一样的(实际上,后面会介绍,如果使用无界queue的话maximumpoolsize参数是没有意义的),keepalivetime和unit的设值表名什么?-就是该实现不想keep alive!最后的blockingqueue选择了linkedblockingqueue,该queue有一个特点,他是无界的。
public static executorservice newfixedthreadpool(int nthreads) { return new threadpoolexecutor(nthreads, nthreads, 0l, timeunit.milliseconds, new linkedblockingqueue<runnable>());         }
executorservice  newsinglethreadexecutor():单线程
public static executorservice newsinglethreadexecutor() { return new finalizabledelegatedexecutorservice                    (new threadpoolexecutor(1, 1, 0l, timeunit.milliseconds, new linkedblockingqueue<runnable>()));          }
executorservice newcachedthreadpool():无界线程池,可以进行自动线程回收
这个实现就有意思了。首先是无界的线程池,所以我们可以发现maximumpoolsize为big big。其次blockingqueue的选择上使用synchronousqueue。可能对于该blockingqueue有些陌生,简单说:该queue中,每个插入操作必须等待另一个线程的对应移除操作。
 public static executorservice newcachedthreadpool() {   return new threadpoolexecutor(0, integer.max_value, 60l, timeunit.seconds, new synchronousqueue<runnable>());       }
先从blockingqueue<runnable> workqueue这个入参开始说起。在jdk中,其实已经说得很清楚了,一共有三种类型的queue。
所有blockingqueue 都可用于传输和保持提交的任务。可以使用此队列与池大小进行交互:
如果运行的线程少于 corepoolsize,则 executor始终首选添加新的线程,而不进行排队。(如果当前运行的线程小于corepoolsize,则任务根本不会存放,添加到queue中,而是直接抄家伙(thread)开始运行)
如果运行的线程等于或多于 corepoolsize,则 executor始终首选将请求加入队列,而不添加新的线程。
如果无法将请求加入队列,则创建新的线程,除非创建此线程超出 maximumpoolsize,在这种情况下,任务将被拒绝。
queue上的三种类型。
排队有三种通用策略:
直接提交。工作队列的默认选项是 synchronousqueue,它将任务直接提交给线程而不保持它们。在此,如果不存在可用于立即运行任务的线程,则试图把任务加入队列将失败,因此会构造一个新的线程。此策略可以避免在处理可能具有内部依赖性的请求集时出现锁。直接提交通常要求无界 maximumpoolsizes 以避免拒绝新提交的任务。当命令以超过队列所能处理的平均数连续到达时,此策略允许无界线程具有增长的可能性。
无界队列。使用无界队列(例如,不具有预定义容量的 linkedblockingqueue)将导致在所有corepoolsize 线程都忙时新任务在队列中等待。这样,创建的线程就不会超过 corepoolsize。(因此,maximumpoolsize的值也就无效了。)当每个任务完全独立于其他任务,即任务执行互不影响时,适合于使用无界队列;例如,在 web页服务器中。这种排队可用于处理瞬态突发请求,当命令以超过队列所能处理的平均数连续到达时,此策略允许无界线程具有增长的可能性。
有界队列。当使用有限的 maximumpoolsizes时,有界队列(如 arrayblockingqueue)有助于防止资源耗尽,但是可能较难调整和控制。队列大小和最大池大小可能需要相互折衷:使用大型队列和小型池可以最大限度地降低 cpu 使用率、操作系统资源和上下文切换开销,但是可能导致人工降低吞吐量。如果任务频繁阻塞(例如,如果它们是 i/o边界),则系统可能为超过您许可的更多线程安排时间。使用小型队列通常要求较大的池大小,cpu使用率较高,但是可能遇到不可接受的调度开销,这样也会降低吞吐量。  
blockingqueue的选择。
例子一:使用直接提交策略,也即synchronousqueue。
首先synchronousqueue是无界的,也就是说他存数任务的能力是没有限制的,但是由于该queue本身的特性,在某次添加元素后必须等待其他线程取走后才能继续添加。在这里不是核心线程便是新创建的线程,但是我们试想一样下,下面的场景。
我们使用一下参数构造threadpoolexecutor:
 new threadpoolexecutor( 2, 3, 30, timeunit.seconds, new  synchronousqueue<runnable>(), new recorderthreadfactory(cookierecorderpool), new threadpoolexecutor.callerrunspolicy());
当核心线程已经有2个正在运行.
此时继续来了一个任务(a),根据前面介绍的“如果运行的线程等于或多于 corepoolsize,则executor始终首选将请求加入队列,而不添加新的线程。”,所以a被添加到queue中。
又来了一个任务(b),且核心2个线程还没有忙完,ok,接下来首先尝试1中描述,但是由于使用的synchronousqueue,所以一定无法加入进去。
此时便满足了上面提到的“如果无法将请求加入队列,则创建新的线程,除非创建此线程超出maximumpoolsize,在这种情况下,任务将被拒绝。”,所以必然会新建一个线程来运行这个任务。
暂时还可以,但是如果这三个任务都还没完成,连续来了两个任务,第一个添加入queue中,后一个呢?queue中无法插入,而线程数达到了maximumpoolsize,所以只好执行异常策略了。
所以在使用synchronousqueue通常要求maximumpoolsize是无界的,这样就可以避免上述情况发生(如果希望限制就直接使用有界队列)。对于使用synchronousqueue的作用jdk中写的很清楚:此策略可以避免在处理可能具有内部依赖性的请求集时出现锁。
什么意思?如果你的任务a1,a2有内部关联,a1需要先运行,那么先提交a1,再提交a2,当使用synchronousqueue我们可以保证,a1必定先被执行,在a1么有被执行前,a2不可能添加入queue中。
例子二:使用无界队列策略,即linkedblockingqueue
这个就拿newfixedthreadpool来说,根据前文提到的规则:
如果运行的线程少于 corepoolsize,则 executor 始终首选添加新的线程,而不进行排队。那么当任务继续增加,会发生什么呢?
如果运行的线程等于或多于 corepoolsize,则 executor 始终首选将请求加入队列,而不添加新的线程。ok,此时任务变加入队列之中了,那什么时候才会添加新线程呢?
如果无法将请求加入队列,则创建新的线程,除非创建此线程超出 maximumpoolsize,在这种情况下,任务将被拒绝。这里就很有意思了,可能会出现无法加入队列吗?不像synchronousqueue那样有其自身的特点,对于无界队列来说,总是可以加入的(资源耗尽,当然另当别论)。换句说,永远也不会触发产生新的线程!corepoolsize大小的线程数会一直运行,忙完当前的,就从队列中拿任务开始运行。所以要防止任务疯长,比如任务运行的实行比较长,而添加任务的速度远远超过处理任务的时间,而且还不断增加,不一会儿就爆了。
例子三:有界队列,使用arrayblockingqueue。
这个是最为复杂的使用,所以jdk不推荐使用也有些道理。与上面的相比,最大的特点便是可以防止资源耗尽的情况发生。
举例来说,请看如下构造方法:
 new threadpoolexecutor( 2, 4, 30, timeunit.seconds, new arrayblockingqueue<runnable>(2), new recorderthreadfactory(cookierecorderpool), new threadpoolexecutor.callerrunspolicy());
假设,所有的任务都永远无法执行完。
对于首先来的a,b来说直接运行,接下来,如果来了c,d,他们会被放到queue中,如果接下来再来e,f,则增加线程运行e,f。但是如果再来任务,队列无法再接受了,线程数也到达最大的限制了,所以就会使用拒绝策略来处理。
keepalivetime
jdk中的解释是:当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。
有点拗口,其实这个不难理解,在使用了“池”的应用中,大多都有类似的参数需要配置。比如数据库连接池,dbcp中的maxidle,minidle参数。
什么意思?接着上面的解释,后来向老板派来的工人始终是“借来的”,俗话说“有借就有还”,但这里的问题就是什么时候还了,如果借来的工人刚完成一个任务就还回去,后来发现任务还有,那岂不是又要去借?这一来一往,老板肯定头也大死了。
合理的策略:既然借了,那就多借一会儿。直到“某一段”时间后,发现再也用不到这些工人时,便可以还回去了。这里的某一段时间便是keepalivetime的含义,timeunit为keepalivetime值的度量。
rejectedexecutionhandler
另一种情况便是,即使向老板借了工人,但是任务还是继续过来,还是忙不过来,这时整个队伍只好拒绝接受了。
rejectedexecutionhandler接口提供了对于拒绝任务的处理的自定方法的机会。在threadpoolexecutor中已经默认包含了4中策略,因为源码非常简单,这里直接贴出来。
callerrunspolicy:线程调用运行该任务的 execute 本身。此策略提供简单的反馈控制机制,能够减缓新任务的提交速度。
public void rejectedexecution(runnable r, threadpoolexecutor e) { if (!e.isshutdown()) {               r.run();           }       }
这个策略显然不想放弃执行任务。但是由于池中已经没有任何资源了,那么就直接使用调用该execute的线程本身来执行。
abortpolicy:处理程序遭到拒绝将抛出运行时rejectedexecutionexception
 这种策略直接抛出异常,丢弃任务。
discardpolicy:不能执行的任务将被删除
public void rejectedexecution(runnable r, threadpoolexecutor e) {       }
这种策略和abortpolicy几乎一样,也是丢弃任务,只不过他不抛出异常。
discardoldestpolicy:如果执行程序尚未关闭,则位于工作队列头部的任务将被删除,然后重试执行程序(如果再次失败,则重复此过程)
public void rejectedexecution(runnable r, threadpoolexecutor e) { if (!e.isshutdown()) {               e.getqueue().poll();               e.execute(r);           }       }
该策略就稍微复杂一些,在pool没有关闭的前提下首先丢掉缓存在队列中的最早的任务,然后重新尝试运行该任务。这个策略需要适当小心。
设想:如果其他线程都还在运行,那么新来任务踢掉旧任务,缓存在queue中,再来一个任务又会踢掉queue中最老任务。
总结:keepalivetime和maximumpoolsize及blockingqueue的类型均有关系。如果blockingqueue是无界的,那么永远不会触发maximumpoolsize,自然keepalivetime也就没有了意义。
反之,如果核心数较小,有界blockingqueue数值又较小,同时keepalivetime又设的很小,如果任务频繁,那么系统就会频繁的申请回收线程。
public static executorservice newfixedthreadpool(int nthreads) { return new threadpoolexecutor(nthreads, nthreads, 0l, timeunit.milliseconds, new linkedblockingqueue<runnable>());}
以上就是java线程池是什么?java线程池的详细讲解的详细内容。
其它类似信息

推荐信息