-
本质:内部封装了ThreadPoolExecutor,根据参数的不同产生了不同的效果。
ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)
-
newFixedThreadPool
定长线程池,每当提交一个任务就创建一个线程,直到达到线程池的最大数量,这时线程数量不再变化,当线程发生错误结束时,线程池会补充一个新的线程
package com.ccssoft.platform.cas.authentication.out.webflow.controller;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestThreadPool {
//定长线程池:每当提交一个任务就创建一个线程,直到达到线程池的最大数量为止;这时线程池数量不再变化,当某个线程发生错误结束时,线程池会补充一个新的线程。超出的线程会在队列中等待。
static ExecutorService fixedExecutor = Executors.newFixedThreadPool(5);
public static void main(String[] args) {
testFixedExecutor();
}
//测试定长线程池,线程池的容量为5,提交11个任务,根据打印结果可以看出先执行前5个任务,5个任务结束后再执行后5任务,再利用线程池空闲线程执行剩下1个。
private static void testFixedExecutor() {
for (int i = 0; i < 11; i++) {
final int index = i;
fixedExecutor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(index == 4){
//int i = 1/0;
}
System.out.println(Thread.currentThread().getName() + " index:" + index);
}
});
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4秒后...");
fixedExecutor.shutdown();
}
}
打印结果:
正常情况打印如下:
pool-1-thread-1 index:0
pool-1-thread-3 index:2
pool-1-thread-2 index:1
pool-1-thread-4 index:3
pool-1-thread-5 index:4
4秒后...
pool-1-thread-1 index:5
pool-1-thread-2 index:7
pool-1-thread-5 index:9
pool-1-thread-3 index:6
pool-1-thread-4 index:8
pool-1-thread-1 index:10
------------------------------------
出现异常情况打印如下:【放开int i = 1/0代码:pool-1-thread-5】
pool-1-thread-1 index:0
Exception in thread "pool-1-thread-5" java.lang.ArithmeticException: / by zero
at com.ccssoft.platform.cas.authentication.out.webflow.controller.TestThreadPool$1.run(TestThreadPool.java:28)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
pool-1-thread-2 index:1
pool-1-thread-3 index:2
pool-1-thread-4 index:3
4秒后...
pool-1-thread-6 index:9【出现异常新建一个补充:pool-1-thread-6】
pool-1-thread-1 index:5
pool-1-thread-3 index:7
pool-1-thread-2 index:6
pool-1-thread-4 index:8
pool-1-thread-6 index:10
-
newCachedThreadPool
可缓存的线程池,如果线程池的容量超过了任务数,自动回收空闲线程,任务增加时可以自动添加新线程,线程池的容量不限制。
package com.ccssoft.platform.cas.authentication.out.webflow.controller;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestThreadPool2 {
//可缓存的线程池,如果线程池的容量超过了任务数,自动回收空闲线程,任务增加时可以自动添加新线程,线程池的容量不限制
static ExecutorService cachedExecutor = Executors.newCachedThreadPool();
public static void main(String[] args) {
testCachedExecutor();
}
//测试可缓存线程池
private static void testCachedExecutor() {
for (int i = 0; i < 7; i++) {
final int index = i;
cachedExecutor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(index == 4){
//int i = 1/0;
}
System.out.println(Thread.currentThread().getName() + " index:" + index);
}
});
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4秒后...");
cachedExecutor.shutdown();
}
}
打印结果:
正常情况:
pool-1-thread-7 index:6
pool-1-thread-6 index:5
pool-1-thread-1 index:0
pool-1-thread-5 index:4
pool-1-thread-2 index:1
pool-1-thread-4 index:3
pool-1-thread-3 index:2
4秒后...
------------------------------------
异常情况:
pool-1-thread-1 index:0
pool-1-thread-2 index:1
pool-1-thread-3 index:2
pool-1-thread-6 index:5
pool-1-thread-7 index:6
pool-1-thread-4 index:3
Exception in thread "pool-1-thread-5" java.lang.ArithmeticException: / by zero
at com.ccssoft.platform.cas.authentication.out.webflow.controller.TestThreadPool2$1.run(TestThreadPool2.java:27)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
4秒后...
-
newScheduledThreadPool
定长线程池,可执行周期性的任务
package com.ccssoft.platform.cas.authentication.out.webflow.controller;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TestThreadPool3 {
//定长线程池,可执行周期性的任务
static ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(3);
public static void main(String[] args) {
testScheduledExecutor();
}
//测试定长、可周期执行的线程池
private static void testScheduledExecutor() {
for (int i = 0; i < 5; i++) {
final int index = i;
//scheduleWithFixedDelay 固定的延迟时间执行任务; scheduleAtFixedRate 固定的频率执行任务
scheduledExecutor.scheduleWithFixedDelay(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + " index:" + index);
}
}, 0, 3, TimeUnit.SECONDS);
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4秒后...");
scheduledExecutor.shutdown();
}
}
执行结果:
pool-1-thread-1 index:0
pool-1-thread-3 index:2
pool-1-thread-2 index:1
pool-1-thread-3 index:4
pool-1-thread-1 index:3
pool-1-thread-3 index:2
pool-1-thread-1 index:1
pool-1-thread-3 index:4
pool-1-thread-1 index:3
pool-1-thread-2 index:0
4秒后...
-
newSingleThreadExecutor
单线程的线程池,线程异常结束,会创建一个新的线程,能确保任务按提交顺序执行
package com.ccssoft.platform.cas.authentication.out.webflow.controller;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestThreadPool4 {
//单线程的线程池,线程异常结束,会创建一个新的线程,能确保任务按提交顺序执行
static ExecutorService singleExecutor = Executors.newSingleThreadExecutor();
public static void main(String[] args) {
testSingleExecutor();
}
//测试单线程的线程池
private static void testSingleExecutor() {
for (int i = 0; i < 5; i++) {
final int index = i;
singleExecutor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(index == 1){
//int t = 1/0;
}
System.out.println(Thread.currentThread().getName() + " index:" + index);
}
});
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4秒后...");
singleExecutor.shutdown();
}
}
执行结果:
正常情况:
pool-1-thread-1 index:0
4秒后...
pool-1-thread-1 index:1
pool-1-thread-1 index:2
pool-1-thread-1 index:3
pool-1-thread-1 index:4
---------------------------
异常情况:
pool-1-thread-1 index:0
4秒后...
Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
at com.ccssoft.platform.cas.authentication.out.webflow.controller.TestThreadPool4$1.run(TestThreadPool4.java:28)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
pool-1-thread-2 index:2
pool-1-thread-2 index:3
pool-1-thread-2 index:4
Surpries! 奉送一份大礼【源码】!!↓↓↓
package java.util.concurrent;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedActionException;
import java.security.AccessControlException;
import sun.security.util.SecurityConstants;
public class Executors {
/*本质:public ThreadPoolExecutor(int corePoolSize, //核心线程数
int maximumPoolSize, //最大线程池数
long keepAliveTime, //存活时间
TimeUnit unit, //存活时间的单位
BlockingQueue<Runnable> workQueue,//阻塞队列BlockingQueue接口的具体实现
ThreadFactory threadFactory, //线程工厂
RejectedExecutionHandler handler //拒绝策略
) {...}
*/
/* 创建一个线程池,该线程池重用在共享的无边界队列上运行的固定数量的线程。在任何时候,线程大部分都是活跃的处理任务。如果在所有线程都处于活动状态时提交其他任务,则它们将在队列中等待,直到有线程可用。如果任何线程在关闭之前由于执行过程中的失败而终止,则在需要执行后续任务时,将替换一个新的线程。池中的线程将一直存在,直到它显式的调用shutdown()方法。
@param nthreads 池中的线程数;如果nthreads<=0,则引发IllegalArgumentException
@return 返回新创建的线程池
*/
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
}
/* 创建一个线程池,该线程池重用在共享的无边界队列上运行的固定数量的线程,并在需要时使用提供的线程工厂创建新线程。在任何时候,线程大部分都是活跃的处理任务。如果在所有线程都处于活动状态时提交了其他任务,则它们将在队列中等待,直到有一个线程可用。如果任何线程在关闭之前由于执行过程中的失败而终止,则在需要执行后续任务时,将替换一个新的线程。池中的线程将存在,直到它显式ExecutorService.shutdown()为止。
@param nthreads池中的线程数 如果nthreads<=0,则引发IllegalArgumentException
@param threadFactory 创建新线程时要使用的工厂;如果ThreadFactory为空,则引发NullPointerException
@return 返回新创建的线程池
*/
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(),threadFactory);
}
/* 创建一个执行器,该执行器使用一个工作线程对一个无边界队列进行操作。(但是,请注意,如果在关闭前执行过程中由于失败而导致此单线程终止,则在需要执行后续任务时将替换一个新线程。)任务保证按顺序执行,并且在任何给定时间都不会有多个任务处于活动状态。与其他等效的newFixedThreadPool(1)不同,返回的执行器保证不会重新配置为使用其他线程。
@return 返回新创建的单线程执行器
*/
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()));
}
/* 创建一个执行器,该执行器使用一个从无边界队列操作的工作线程,并在需要时使用提供的ThreadFactory创建一个新线程。与其他等效的newFixedThreadPool(1,threadFactory)不同,返回的执行器保证不会重新配置为使用其他线程。
@param threadFactory 创建新线程时要使用的工厂;如果ThreadFactory为空,则引发NullPointerException
@return 返回新创建的线程池
*/
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(),threadFactory));
}
/* 创建一个线程池,该线程池根据需要创建新线程,但在先前构建的线程可用时将重用它们。这些池通常会提高执行许多短期异步任务的程序的性能。调用execute将重用以前构造的线程(如果可用)。如果没有现有线程可用,将创建一个新线程并将其添加到池中。60秒未使用的线程将被终止并从缓存中删除。因此,一个足够长时间闲置的池将不会消耗任何资源。
@return 返回新创建的线程池
*/
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
}
/*
创建一个线程池,该线程池根据需要创建新线程,但在先前构建的线程可用时将重用它们,并在需要时使用提供的threadFactory创建新线程。
@param threadFactory 创建新线程时要使用的工厂;如果ThreadFactory为空,则引发NullPointerException
@return 返回新创建的线程池
*/
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>(),threadFactory);
}
/* 创建一个单线程执行器,该执行器可以调度命令在给定延迟后运行,或者定期执行。(但是,请注意,如果在关闭前执行过程中由于失败而导致此单线程终止,则在需要执行后续任务时将替换一个新线程。)任务保证按顺序执行,并且在任何给定时间都不会有多个任务处于活动状态。与其他等效的newscheduledthreadpool(1)不同,返回的执行器保证不会重新配置为使用其他线程。
@return 返回新创建的计划执行者
*/
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
}
/* 创建一个单线程执行器,该执行器可以调度命令在给定延迟后运行,或者定期执行。(但是,请注意,如果在关闭前执行过程中由于失败而导致此单线程终止,则在需要执行后续任务时将替换一个新线程。)任务保证按顺序执行,并且在任何给定时间都不会有多个任务处于活动状态。与其他等效的newscheduledThreadPool(1,threadFactory)不同,返回的执行器保证不会重新配置为使用其他线程。
@创建新工厂时要使用的工厂 如果ThreadFactory为空,则引发NullPointerException
@返回新创建的计划执行者
*/
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
return new DelegatedScheduledExecutorService(new ScheduledThreadPoolExecutor(1, threadFactory));
}
/*
创建一个线程池,该线程池可以安排命令在给定延迟后运行,或者定期执行。
@param corepoolsize要保留在池中的线程数,即使它们是空闲的。如果corepoolsize<0,则引发IllegalArgumentException
@返回新创建的计划线程池
*/
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
/*
创建一个线程池,该线程池可以安排命令在给定延迟后运行,或者定期执行。
@param corePoolSize corepoolsize要保留在池中的线程数,即使它们是空闲的。throws IllegalArgumentException if corePoolSize < 0
@param threadFactory 执行器创建新线程时要使用的工厂。throws NullPointerException if threadFactory is null
@return 新创建的计划线程池
*/
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
//不能实例化
private Executors() {}
}