四种拒绝策略:
拒绝策略类型 | 说明 |
---|---|
ThreadPoolExecutor.AbortPolicy | 默认拒绝策略,拒绝任务并抛出任务 |
ThreadPoolExecutor.CallerRunsPolicy | 使用调用线程直接运行任务 |
ThreadPoolExecutor.DiscardPolicy | 直接拒绝任务,不抛出错误 |
ThreadPoolExecutor.DiscardOldestPolicy | 触发拒绝策略,只要还有任务新增,一直会丢弃阻塞队列的最老的任务,并将新的任务加入 |
验证:
package com.thread.test;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author zhangcj
* @date 2023/5/24 14:29
*/
public class TestThreadPoolExecutor {
public static void main(String[] args) {
//默认拒绝策略,拒绝任务并抛出任务
ThreadPoolExecutor.AbortPolicy abortPolicy = new ThreadPoolExecutor.AbortPolicy();
//使用调用线程直接运行任务
ThreadPoolExecutor.CallerRunsPolicy callerRunsPolicy = new ThreadPoolExecutor.CallerRunsPolicy();
//直接拒绝任务,不抛出错误
ThreadPoolExecutor.DiscardPolicy discardPolicy = new ThreadPoolExecutor.DiscardPolicy();
//触发拒绝策略,只要还有任务新增,一直会丢弃阻塞队列的最老的任务,并将新的任务加入
ThreadPoolExecutor.DiscardOldestPolicy discardOldestPolicy = new ThreadPoolExecutor.DiscardOldestPolicy();
ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1),abortPolicy );
// 任务1
pool.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3 * 1000);
System.out.println("-------------hello world_001---------------" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// 任务2
pool.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5 * 1000);
System.out.println("-------------hello world_002---------------" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// 任务3
pool.execute(new Runnable() {
@Override
public void run() {
System.out.println("-------------hello world_003---------------" + Thread.currentThread().getName());
}
});
// 任务4
pool.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("-------------hello world_004---------------" + Thread.currentThread().getName());
}
});
// 任务5
pool.execute(new Runnable() {
@Override
public void run() {
System.out.println("-------------hello world_005---------------" + Thread.currentThread().getName());
}
});
}
}
1、ThreadPoolExecutor.AbortPolicy 默认拒绝策略,拒绝任务并抛出任务(日志输出)
Connected to the target VM, address: '127.0.0.1:56382', transport: 'socket'
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.thread.test.TestThreadPoolExecutor$5@277c0f21 rejected from java.util.concurrent.ThreadPoolExecutor@7a46a697[Running, pool size = 3, active threads = 3, queued tasks = 1, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at com.thread.test.TestThreadPoolExecutor.main(TestThreadPoolExecutor.java:73)
-------------helloworld_004---------------pool-1-thread-3
-------------helloworld_003---------------pool-1-thread-3
-------------helloworld_001---------------pool-1-thread-1
-------------helloworld_002---------------pool-1-thread-2
2、ThreadPoolExecutor.CallerRunsPolicy 使用调用线程直接运行任务(日志输出)
Connected to the target VM, address: '127.0.0.1:56109', transport: 'socket'
-------------helloworld_005---------------main
-------------helloworld_004---------------pool-1-thread-3
-------------helloworld_003---------------pool-1-thread-3
-------------helloworld_001---------------pool-1-thread-1
-------------helloworld_002---------------pool-1-thread-2
3、ThreadPoolExecutor.DiscardPolicy 直接拒绝任务,不抛出错误(日志输出)
Connected to the target VM, address: '127.0.0.1:56421', transport: 'socket'
-------------helloworld_004---------------pool-1-thread-3
-------------helloworld_003---------------pool-1-thread-3
-------------helloworld_001---------------pool-1-thread-1
-------------helloworld_002---------------pool-1-thread-2
4、ThreadPoolExecutor.DiscardOldestPolicy 触发拒绝策略,只要还有任务新增,一直会丢弃阻塞队列的最老的任务,并将新的任务加入(日志输出)
package com.thread.test;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author zhangcj
* @date 2023/5/24 14:29
*/
public class TestThreadPoolExecutor {
public static void main(String[] args) {
//默认拒绝策略,拒绝任务并抛出任务
ThreadPoolExecutor.AbortPolicy abortPolicy = new ThreadPoolExecutor.AbortPolicy();
//使用调用线程直接运行任务
ThreadPoolExecutor.CallerRunsPolicy callerRunsPolicy = new ThreadPoolExecutor.CallerRunsPolicy();
//直接拒绝任务,不抛出错误
ThreadPoolExecutor.DiscardPolicy discardPolicy = new ThreadPoolExecutor.DiscardPolicy();
//触发拒绝策略,只要还有任务新增,一直会丢弃阻塞队列的最老的任务,并将新的任务加入
ThreadPoolExecutor.DiscardOldestPolicy discardOldestPolicy = new ThreadPoolExecutor.DiscardOldestPolicy();
ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1),discardOldestPolicy);
// 任务1
pool.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(9 * 1000);
System.out.println("-------------helloworld_001---------------" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// 任务2
pool.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5 * 1000);
System.out.println("-------------helloworld_002---------------" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// 任务3
pool.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3 * 1000);
System.out.println("-------------helloworld_003---------------" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// 任务4
pool.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("-------------helloworld_004---------------" + Thread.currentThread().getName());
}
});
// 任务5
pool.execute(new Runnable() {
@Override
public void run() {
System.out.println("-------------helloworld_005---------------" + Thread.currentThread().getName());
}
});
// 任务6
pool.execute(new Runnable() {
@Override
public void run() {
System.out.println("-------------helloworld_006---------------" + Thread.currentThread().getName());
}
});
}
}
日志输出:
Connected to the target VM, address: '127.0.0.1:56598', transport: 'socket'
-------------helloworld_004---------------pool-1-thread-3
-------------helloworld_006---------------pool-1-thread-3
-------------helloworld_002---------------pool-1-thread-2
-------------helloworld_001---------------pool-1-thread-1