feat: 开发线程池统计指标.

pull/161/head
chen.ma 3 years ago
parent ed17f48ddf
commit 2d4c0e19c0

@ -1,26 +1,79 @@
package io.dynamic.threadpool.example.config; package io.dynamic.threadpool.example.config;
import io.dynamic.threadpool.starter.core.GlobalThreadPoolManage;
import io.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; import io.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** /**
* 线 * 线
* *
* @author chen.ma * @author chen.ma
* @date 2021/6/20 17:16 * @date 2021/6/20 17:16
*/ */
@Slf4j
@Configuration @Configuration
public class ThreadPoolConfig { public class ThreadPoolConfig {
private String messageConsumePrefix = "message-consume";
private String messageProducePrefix = "message-produce";
private String customPoolPrefix = "custom-pool";
@Bean @Bean
public DynamicThreadPoolWrap messageCenterConsumeThreadPool() { public DynamicThreadPoolWrap messageCenterConsumeThreadPool() {
return new DynamicThreadPoolWrap("message-consume"); return new DynamicThreadPoolWrap(messageConsumePrefix);
} }
@Bean @Bean
public DynamicThreadPoolWrap messageCenterProduceThreadPool() { public DynamicThreadPoolWrap messageCenterProduceThreadPool() {
return new DynamicThreadPoolWrap("message-produce"); return new DynamicThreadPoolWrap(messageProducePrefix);
}
@Bean
public DynamicThreadPoolWrap customPool() {
return new DynamicThreadPoolWrap(customPoolPrefix);
} }
@PostConstruct
@SuppressWarnings("all")
public void testExecuteTask() {
log.info("测试线程池运行时状态接口, 30s 后开始触发拒绝策略...");
ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor();
scheduledThreadPool.scheduleAtFixedRate(() -> {
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(customPoolPrefix);
ThreadPoolExecutor pool = executorService.getPool();
try {
pool.execute(() -> {
log.info("线程池名称 :: {}, 正在执行即将进入阻塞...", Thread.currentThread().getName());
try {
int maxRandom = 10;
int temp = 2;
Random random = new Random();
// 这里为了赋值线程池 completedTaskCount
if (random.nextInt(maxRandom) % temp == 0) {
Thread.sleep(10241024);
} else {
Thread.sleep(3000);
}
} catch (InterruptedException e) {
// ignore
}
});
} catch (Exception ex) {
// ignore
}
}, 5, 2, TimeUnit.SECONDS);
}
} }

Loading…
Cancel
Save