From e7be8acca8c05ca799131ed55292123254528b40 Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Tue, 13 Sep 2022 21:01:48 +0800 Subject: [PATCH] Add built-in deny policy documentation --- .../dev_manual/blocking-queue-custom.md | 2 +- .../dev_manual/rejected-policy-custom.md | 2 +- .../dev_manual/rejected-policy-info.md | 50 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 docs/docs/user_docs/dev_manual/rejected-policy-info.md diff --git a/docs/docs/user_docs/dev_manual/blocking-queue-custom.md b/docs/docs/user_docs/dev_manual/blocking-queue-custom.md index 3ab0d33c..67c8df6b 100644 --- a/docs/docs/user_docs/dev_manual/blocking-queue-custom.md +++ b/docs/docs/user_docs/dev_manual/blocking-queue-custom.md @@ -1,5 +1,5 @@ --- -sidebar_position: 1 +sidebar_position: 2 --- # 阻塞队列自定义 diff --git a/docs/docs/user_docs/dev_manual/rejected-policy-custom.md b/docs/docs/user_docs/dev_manual/rejected-policy-custom.md index 970511f5..53b1d78f 100644 --- a/docs/docs/user_docs/dev_manual/rejected-policy-custom.md +++ b/docs/docs/user_docs/dev_manual/rejected-policy-custom.md @@ -1,5 +1,5 @@ --- -sidebar_position: 0 +sidebar_position: 1 --- # 拒绝策略自定义 diff --git a/docs/docs/user_docs/dev_manual/rejected-policy-info.md b/docs/docs/user_docs/dev_manual/rejected-policy-info.md new file mode 100644 index 00000000..d5c68138 --- /dev/null +++ b/docs/docs/user_docs/dev_manual/rejected-policy-info.md @@ -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 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); + } + } +} +``` \ No newline at end of file