优化动态线程池创建以及监控代码.

pull/28/head
chen.ma 3 years ago
parent 5b5eabab7e
commit 5f75eb5f3c

@ -2,7 +2,7 @@ package cn.hippo4j.starter.core;
import cn.hippo4j.starter.alarm.ThreadPoolAlarm; import cn.hippo4j.starter.alarm.ThreadPoolAlarm;
import cn.hippo4j.starter.alarm.ThreadPoolAlarmManage; import cn.hippo4j.starter.alarm.ThreadPoolAlarmManage;
import cn.hippo4j.starter.event.EventExecutor; import cn.hippo4j.starter.event.MonitorEventExecutor;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.core.task.TaskDecorator; import org.springframework.core.task.TaskDecorator;
@ -312,7 +312,7 @@ public class DynamicThreadPoolExecutor extends DynamicExecutorConfigurationSuppo
final void reject(Runnable command) { final void reject(Runnable command) {
rejectCount.incrementAndGet(); rejectCount.incrementAndGet();
EventExecutor.publishEvent( MonitorEventExecutor.publishEvent(
() -> ThreadPoolAlarmManage.checkPoolRejectAlarm(this) () -> ThreadPoolAlarmManage.checkPoolRejectAlarm(this)
); );
handler.rejectedExecution(command, this); handler.rejectedExecution(command, this);
@ -367,7 +367,7 @@ public class DynamicThreadPoolExecutor extends DynamicExecutorConfigurationSuppo
} }
} }
EventExecutor.publishEvent( MonitorEventExecutor.publishEvent(
() -> ThreadPoolAlarmManage.checkPoolLivenessAlarm(core, this) () -> ThreadPoolAlarmManage.checkPoolLivenessAlarm(core, this)
); );
@ -548,7 +548,7 @@ public class DynamicThreadPoolExecutor extends DynamicExecutorConfigurationSuppo
c = ctl.get(); c = ctl.get();
} }
if (isRunning(c) && workQueue.offer(command)) { if (isRunning(c) && workQueue.offer(command)) {
EventExecutor.publishEvent( MonitorEventExecutor.publishEvent(
() -> ThreadPoolAlarmManage.checkPoolCapacityAlarm(this) () -> ThreadPoolAlarmManage.checkPoolCapacityAlarm(this)
); );
int recheck = ctl.get(); int recheck = ctl.get();

@ -46,10 +46,14 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor {
private final ThreadPoolOperation threadPoolOperation; private final ThreadPoolOperation threadPoolOperation;
private final ExecutorService executorService = ThreadPoolBuilder.builder() private final ExecutorService executorService = ThreadPoolBuilder.builder()
.poolThreadSize(2, 4) .corePoolSize(2)
.keepAliveTime(0L, TimeUnit.MILLISECONDS) .maxPoolNum(4)
.workQueue(QueueTypeEnum.ARRAY_BLOCKING_QUEUE, 1) .keepAliveTime(2000)
.threadFactory("dynamic-threadPool-config") .timeUnit(TimeUnit.MILLISECONDS)
.workQueue(QueueTypeEnum.ARRAY_BLOCKING_QUEUE)
.capacity(1)
.allowCoreThreadTimeOut(true)
.threadFactory("dynamic-threadPool-init-config")
.rejected(new ThreadPoolExecutor.DiscardOldestPolicy()) .rejected(new ThreadPoolExecutor.DiscardOldestPolicy())
.build(); .build();

@ -1,35 +0,0 @@
package cn.hippo4j.starter.event;
import cn.hippo4j.common.function.NoArgsConsumer;
import cn.hippo4j.starter.toolkit.thread.QueueTypeEnum;
import cn.hippo4j.starter.toolkit.thread.ThreadPoolBuilder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
/**
* .
*
* @author chen.ma
* @date 2021/11/8 23:44
*/
public class EventExecutor {
private static final ExecutorService EVENT_EXECUTOR = ThreadPoolBuilder.builder()
.threadFactory("event-executor")
.corePoolSize(Runtime.getRuntime().availableProcessors())
.maxPoolNum(Runtime.getRuntime().availableProcessors())
.workQueue(QueueTypeEnum.ARRAY_BLOCKING_QUEUE, 2048)
.rejected(new ThreadPoolExecutor.DiscardPolicy())
.build();
/**
* .
*
* @param consumer
*/
public static void publishEvent(NoArgsConsumer consumer) {
EVENT_EXECUTOR.execute(consumer::accept);
}
}

@ -0,0 +1,45 @@
package cn.hippo4j.starter.event;
import cn.hippo4j.common.function.NoArgsConsumer;
import cn.hippo4j.starter.toolkit.thread.QueueTypeEnum;
import cn.hippo4j.starter.toolkit.thread.ThreadPoolBuilder;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import static cn.hippo4j.common.constant.Constants.AVAILABLE_PROCESSORS;
/**
* 线.
*
* @author chen.ma
* @date 2021/11/8 23:44
*/
@Slf4j
public class MonitorEventExecutor {
private static final ExecutorService EVENT_EXECUTOR = ThreadPoolBuilder.builder()
.threadFactory("monitor-event-executor")
.corePoolSize(AVAILABLE_PROCESSORS)
.maxPoolNum(AVAILABLE_PROCESSORS)
.workQueue(QueueTypeEnum.LINKED_BLOCKING_QUEUE)
.capacity(4096)
.rejected(new ThreadPoolExecutor.AbortPolicy())
.build();
/**
* .
*
* @param consumer
*/
public static void publishEvent(NoArgsConsumer consumer) {
try {
EVENT_EXECUTOR.execute(consumer::accept);
} catch (RejectedExecutionException ex) {
log.error("Monitoring thread pool run events exceeded load.");
}
}
}

@ -49,14 +49,20 @@ public class AbstractBuildThreadPoolTemplate {
*/ */
public static ThreadPoolExecutor buildPool(ThreadPoolInitParam initParam) { public static ThreadPoolExecutor buildPool(ThreadPoolInitParam initParam) {
Assert.notNull(initParam); Assert.notNull(initParam);
ThreadPoolExecutor executorService = ThreadPoolExecutor executorService;
new ThreadPoolExecutorTemplate(initParam.getCorePoolNum(), try {
initParam.getMaxPoolNum(), executorService = new ThreadPoolExecutorTemplate(initParam.getCorePoolNum(),
initParam.getKeepAliveTime(), initParam.getMaxPoolNum(),
initParam.getTimeUnit(), initParam.getKeepAliveTime(),
initParam.getWorkQueue(), initParam.getTimeUnit(),
initParam.getThreadFactory(), initParam.getWorkQueue(),
initParam.rejectedExecutionHandler); initParam.getThreadFactory(),
initParam.rejectedExecutionHandler);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Error creating thread pool parameter.", ex);
}
executorService.allowCoreThreadTimeOut(initParam.allowCoreThreadTimeOut);
return executorService; return executorService;
} }
@ -77,15 +83,21 @@ public class AbstractBuildThreadPoolTemplate {
*/ */
public static ThreadPoolExecutor buildFastPool(ThreadPoolInitParam initParam) { public static ThreadPoolExecutor buildFastPool(ThreadPoolInitParam initParam) {
TaskQueue<Runnable> taskQueue = new TaskQueue(initParam.getCapacity()); TaskQueue<Runnable> taskQueue = new TaskQueue(initParam.getCapacity());
FastThreadPoolExecutor fastThreadPoolExecutor = FastThreadPoolExecutor fastThreadPoolExecutor;
new FastThreadPoolExecutor(initParam.getCorePoolNum(), try {
initParam.getMaxPoolNum(), fastThreadPoolExecutor = new FastThreadPoolExecutor(initParam.getCorePoolNum(),
initParam.getKeepAliveTime(), initParam.getMaxPoolNum(),
initParam.getTimeUnit(), initParam.getKeepAliveTime(),
taskQueue, initParam.getTimeUnit(),
initParam.getThreadFactory(), taskQueue,
initParam.rejectedExecutionHandler); initParam.getThreadFactory(),
initParam.rejectedExecutionHandler);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Error creating thread pool parameter.", ex);
}
taskQueue.setExecutor(fastThreadPoolExecutor); taskQueue.setExecutor(fastThreadPoolExecutor);
fastThreadPoolExecutor.allowCoreThreadTimeOut(initParam.allowCoreThreadTimeOut);
return fastThreadPoolExecutor; return fastThreadPoolExecutor;
} }
@ -97,21 +109,28 @@ public class AbstractBuildThreadPoolTemplate {
*/ */
public static DynamicThreadPoolExecutor buildDynamicPool(ThreadPoolInitParam initParam) { public static DynamicThreadPoolExecutor buildDynamicPool(ThreadPoolInitParam initParam) {
Assert.notNull(initParam); Assert.notNull(initParam);
DynamicThreadPoolExecutor executorService = DynamicThreadPoolExecutor dynamicThreadPoolExecutor;
new DynamicThreadPoolExecutor(initParam.getCorePoolNum(), try {
initParam.getMaxPoolNum(), dynamicThreadPoolExecutor = new DynamicThreadPoolExecutor(
initParam.getKeepAliveTime(), initParam.getCorePoolNum(),
initParam.getTimeUnit(), initParam.getMaxPoolNum(),
initParam.getWaitForTasksToCompleteOnShutdown(), initParam.getKeepAliveTime(),
initParam.getAwaitTerminationMillis(), initParam.getTimeUnit(),
initParam.getWorkQueue(), initParam.getWaitForTasksToCompleteOnShutdown(),
initParam.getThreadPoolId(), initParam.getAwaitTerminationMillis(),
initParam.getThreadFactory(), initParam.getWorkQueue(),
initParam.getThreadPoolAlarm(), initParam.getThreadPoolId(),
initParam.getRejectedExecutionHandler()); initParam.getThreadFactory(),
initParam.getThreadPoolAlarm(),
executorService.setTaskDecorator(initParam.getTaskDecorator()); initParam.getRejectedExecutionHandler()
return executorService; );
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException(String.format("Error creating thread pool parameter. threadPool id :: %s", initParam.getThreadPoolId()), ex);
}
dynamicThreadPoolExecutor.setTaskDecorator(initParam.getTaskDecorator());
dynamicThreadPoolExecutor.allowCoreThreadTimeOut(initParam.allowCoreThreadTimeOut);
return dynamicThreadPoolExecutor;
} }
@Data @Data
@ -183,6 +202,11 @@ public class AbstractBuildThreadPoolTemplate {
*/ */
private Boolean waitForTasksToCompleteOnShutdown; private Boolean waitForTasksToCompleteOnShutdown;
/**
* 线
*/
private Boolean allowCoreThreadTimeOut = false;
public ThreadPoolInitParam(String threadNamePrefix, boolean isDaemon) { public ThreadPoolInitParam(String threadNamePrefix, boolean isDaemon) {
this.threadPoolId = threadNamePrefix; this.threadPoolId = threadNamePrefix;
this.threadFactory = ThreadFactoryBuilder.builder() this.threadFactory = ThreadFactoryBuilder.builder()

@ -112,6 +112,11 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
*/ */
private Boolean waitForTasksToCompleteOnShutdown = true; private Boolean waitForTasksToCompleteOnShutdown = true;
/**
* 线
*/
private Boolean allowCoreThreadTimeOut = false;
/** /**
* CPU / (1 - 0.8) * CPU / (1 - 0.8)
* *
@ -234,6 +239,11 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
return this; return this;
} }
public ThreadPoolBuilder allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
this.allowCoreThreadTimeOut = allowCoreThreadTimeOut;
return this;
}
/** /**
* *
* *
@ -303,6 +313,7 @@ public class ThreadPoolBuilder implements Builder<ThreadPoolExecutor> {
.setCapacity(builder.capacity) .setCapacity(builder.capacity)
.setRejectedExecutionHandler(builder.rejectedExecutionHandler) .setRejectedExecutionHandler(builder.rejectedExecutionHandler)
.setTimeUnit(builder.timeUnit) .setTimeUnit(builder.timeUnit)
.setAllowCoreThreadTimeOut(builder.allowCoreThreadTimeOut)
.setTaskDecorator(builder.taskDecorator); .setTaskDecorator(builder.taskDecorator);
if (builder.isDynamicPool) { if (builder.isDynamicPool) {

Loading…
Cancel
Save