JDK提供的几种线程池缓存
newFixedThreadPool
建立一个指定工做线程数量的线程池。每当提交一个任务就建立一个工做线程,若是工做线程数量达到线程池初始的最大数,则将提交的任务存入到池队列中。app
newCachedThreadPool
建立一个可缓存的线程池。这种类型的线程池特色是:
1).工做线程的建立数量几乎没有限制(其实也有限制的,数目为Interger. MAX_VALUE), 这样可灵活的往线程池中添加线程。
2).若是长时间没有往线程池中提交任务,即若是工做线程空闲了指定的时间(默认为1分钟),则该工做线程将自动终止。终止后,若是你又提交了新的任务,则线程池从新建立一个工做线程。ui
newSingleThreadExecutor
建立一个单线程化的Executor,即只建立惟一的工做者线程来执行任务,若是这个线程异常结束,会有另外一个取代它,保证顺序执行(我以为这点是它的特点)。单工做线程最大的特色是可保证顺序地执行各个任务,而且在任意给定的时间不会有多个线程是活动的 。this
newScheduleThreadPool
建立一个定长的线程池,并且支持定时的以及周期性的任务执行,相似于Timer。(这种线程池原理暂还没彻底了解透彻).net
总结:
FixedThreadPool
是一个典型且优秀的线程池,它具备线程池提升程序效率和节省建立线程时所耗的开销的优势。可是,在线程池空闲时,即线程池中没有可运行任务时,它不会释放工做线程,还会占用必定的系统资源。线程
CachedThreadPool
特色是在线程池空闲时,即线程池中没有可运行任务时,它会释放工做线程,从而释放工做线程所占用的资源。可是,但当出现新任务时,又要建立一新的工做线程,又要必定的系统开销。而且,在使用CachedThreadPool时,必定要注意控制任务的数量,不然,因为大量线程同时运行,颇有会形成系统瘫痪。orm
线程池工厂类:
Executors
Factory and utility methods for {@link Executor}, {@link ExecutorService}, {@link ScheduledExecutorService}, {@link ThreadFactory}, and {@link Callable} classes defined in this package. This class supports the following kinds of methods:
Methods that create and return an {@link ExecutorService} set up with commonly useful configuration settings.
Methods that create and return a {@link ScheduledExecutorService} set up with commonly useful configuration settings.
Methods that create and return a "wrapped" ExecutorService, that disables reconfiguration by making implementation-specific methods inaccessible.
Methods that create and return a {@link ThreadFactory} that sets newly created threads to a known state.
Methods that create and return a {@link Callable} out of other closure-like forms, so they can be used in execution methods requiring <tt>Callable</tt>.blog
转自:http://blog.csdn.net/it_man/article/details/7193727队列