feat: 新增可计数的拒绝策略.

This commit is contained in:
2025-03-05 14:50:36 +08:00
parent f7e4400e37
commit af9c1c9622
6 changed files with 83 additions and 9 deletions

View File

@@ -9,6 +9,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
@@ -20,6 +21,9 @@ import java.util.function.Consumer;
*/
public abstract class AbstractStateMachine<S> extends AbstractStateManager<S> implements StateMachine<S> {
/**
* 状态机上下文
*/
protected final StateMachineContext<S> context;
/**
@@ -52,6 +56,9 @@ public abstract class AbstractStateMachine<S> extends AbstractStateManager<S> im
if (executor instanceof ExecutorService) {
ExecutorService es = (ExecutorService) executor;
es.shutdown();
if (!es.awaitTermination(10, TimeUnit.SECONDS)) {
es.shutdownNow();
}
} else if (executor instanceof AutoCloseable) {
AutoCloseable ac = (AutoCloseable) executor;
ac.close();

View File

@@ -1,11 +1,15 @@
package com.serliunx.statemanagement.machine;
import com.serliunx.statemanagement.machine.handler.StateHandlerWrapper;
import com.serliunx.statemanagement.support.DefaultCountableRejectedExecutionHandler;
import com.serliunx.statemanagement.support.ExecutorUtils;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
/**
@@ -62,7 +66,7 @@ public final class StateMachineContext<S> {
*/
private Executor executorAutoConfiguration(Executor source) {
if (source == null) {
return ExecutorUtils.adaptiveThreadPool();
return ExecutorUtils.adaptiveThreadPool(new DefaultCountableRejectedExecutionHandler());
}
return source;
}