1、newfixedthreadpool - 定长线程池
创建一个线程池,该线程池重用在共享无界队列上运行的固定数量的线程。
在任何时候,线程最多都是活动的处理任务。如果在所有线程都处于活动状态时提交其他任务,它们将在队列中等待,直到有线程可用。
如果任何线程在关机前的执行过程中由于故障而终止,那么如果需要执行后续任务,将有一个新线程替代它。
池中的线程将一直存在,直到显式关闭。
public static executorservice newfixedthreadpool(int nthreads) { return new threadpoolexecutor(nthreads, nthreads, 0l, timeunit.milliseconds, new linkedblockingqueue<runnable>()); }
public static executorservice newfixedthreadpool(int nthreads, threadfactory threadfactory){ return new threadpoolexecutor(nthreads, nthreads, 0l, timeunit.milliseconds, new linkedblockingqueue<runnable>(), threadfactory); }
2、newsinglethreadexecutor - 单一线程池
创建一个执行器,该执行器使用一个工作线程在无界队列上运行。
(但是请注意,如果此单线程在关机前的执行过程中由于故障而终止,那么如果需要执行后续任务,将使用一个新线程代替它。)
任务保证按顺序执行,并且在任何给定时间都不会有多个任务处于活动状态。
与其他等效的newfixedthreadpool(1)不同,返回的执行器保证不可重新配置以使用其他线程。
public static executorservice newsinglethreadexecutor() { return new finalizabledelegatedexecutorservice (new threadpoolexecutor(1, 1, 0l, timeunit.milliseconds, new linkedblockingqueue<runnable>())); }
public static executorservice newsinglethreadexecutor(threadfactory threadfactory) { return new finalizabledelegatedexecutorservice (new threadpoolexecutor(1, 1, 0l, timeunit.milliseconds, new linkedblockingqueue<runnable>(), threadfactory)); }
newsinglethreadexecutor和newfixedthreadpool(1)区别
3、newcachedthreadpool - 缓存线程池
创建一个线程池,该线程池根据需要创建新线程,但在以前构造的线程可用时将重用这些线程。
这些池通常会提高执行许多短期异步任务的程序的性能。
调用execute将重用以前构造的线程(如果可用)。
如果没有可用的现有线程,将创建一个新线程并将其添加到池中。
60秒未使用的线程将被终止并从缓存中删除。
因此,闲置足够长时间的池不会消耗任何资源。
请注意,可以使用threadpoolexecutor构造函数创建具有类似属性但不同细节(例如超时参数)的池。
public static executorservice newcachedthreadpool() { return new threadpoolexecutor(0, integer.max_value, 60l, timeunit.seconds, new synchronousqueue<runnable>()); }
public static executorservice newcachedthreadpool(threadfactory threadfactory) { return new threadpoolexecutor(0, integer.max_value, 60l, timeunit.seconds, new synchronousqueue<runnable>(), threadfactory); }
4、newscheduledthreadpool - 调度线程池
创建一个线程池,该线程池可以安排命令在给定延迟后运行,或定期执行。
参数:
corepoolsize – 池中要保留的线程数,即使它们处于空闲状态
public static scheduledexecutorservice newscheduledthreadpool(int corepoolsize) { return new scheduledthreadpoolexecutor(corepoolsize); }
public static scheduledexecutorservice newscheduledthreadpool( int corepoolsize, threadfactory threadfactory) { return new scheduledthreadpoolexecutor(corepoolsize, threadfactory); }
5、newsinglethreadscheduledexecutor - 单线程调度线程池
创建一个单线程执行器,该执行器可以安排命令在给定延迟后运行,或定期执行。
(但是请注意,如果此单线程在关机前的执行过程中由于故障而终止,那么如果需要执行后续任务,将使用一个新线程代替它。)
任务保证按顺序执行,并且在任何给定时间都不会有多个任务处于活动状态。
与其他等效的newscheduledthreadpool(1)不同,返回的执行器保证不可重新配置以使用其他线程。
public static scheduledexecutorservice newsinglethreadscheduledexecutor() { return new delegatedscheduledexecutorservice (new scheduledthreadpoolexecutor(1)); }
public static scheduledexecutorservice newsinglethreadscheduledexecutor(threadfactory threadfactory) { return new delegatedscheduledexecutorservice (new scheduledthreadpoolexecutor(1, threadfactory)); }
6、newworkstealingpool - 抢占操作线程池
创建一个线程池,该线程池维护足够多的线程以支持给定的并行度级别,并且可以使用多个队列来减少争用。
并行级别对应于积极参与或可参与任务处理的最大线程数。
线程的实际数量可能会动态增长和收缩。
工作窃取池不保证提交任务的执行顺序。
参数:
并行度——目标并行度级别
public static executorservice newworkstealingpool(int parallelism) { return new forkjoinpool (parallelism, forkjoinpool.defaultforkjoinworkerthreadfactory, null, true); }
public static executorservice newworkstealingpool() { return new forkjoinpool (runtime.getruntime().availableprocessors(), forkjoinpool.defaultforkjoinworkerthreadfactory, null, true); }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论