You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hippo4j/docs/versioned_docs/version-1.4.2/user_docs/dev_manual/rejected-policy-info.md

1.2 KiB

sidebar_position
0

内置拒绝策略

内置两种拒绝策略说明:

RunsOldestTaskPolicy:添加新任务并由主线程运行最早的任务。

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:主线程把拒绝任务以阻塞的方式添加到队列。

@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);
        }
    }
}