Add built-in deny policy documentation

pull/693/head
chen.ma 2 years ago
parent 6fdb72d06d
commit e7be8acca8

@ -1,5 +1,5 @@
---
sidebar_position: 1
sidebar_position: 2
---
# 阻塞队列自定义

@ -1,5 +1,5 @@
---
sidebar_position: 0
sidebar_position: 1
---
# 拒绝策略自定义

@ -0,0 +1,50 @@
---
sidebar_position: 0
---
# 内置拒绝策略
内置两种拒绝策略说明:
**RunsOldestTaskPolicy**:添加新任务并由主线程运行最早的任务。
```java
public class RunsOldestTaskPolicy implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
if (executor.isShutdown()) {
return;
}
BlockingQueue<Runnable> workQueue = executor.getQueue();
Runnable firstWork = workQueue.poll();
boolean newTaskAdd = workQueue.offer(r);
if (firstWork != null) {
firstWork.run();
}
if (!newTaskAdd) {
executor.execute(r);
}
}
}
```
**SyncPutQueuePolicy**:主线程把拒绝任务以阻塞的方式添加到队列。
```java
@Slf4j
public class SyncPutQueuePolicy implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
if (executor.isShutdown()) {
return;
}
try {
executor.getQueue().put(r);
} catch (InterruptedException e) {
log.error("Adding Queue task to thread pool failed.", e);
}
}
}
```
Loading…
Cancel
Save