From 75dd3daef881fa089efd89c7b3f84276974cd297 Mon Sep 17 00:00:00 2001 From: Wonder Date: Mon, 10 Nov 2025 21:57:17 +0800 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=94=84Update:=20=E4=BD=BF=E7=94=A8=20H?= =?UTF-8?q?utool=20=E5=88=9B=E5=BB=BA=E7=BA=BF=E7=A8=8B=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 +++ .../threadpool/ThreadPoolApplication.java | 39 +++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index fa1bbb8..f55943e 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,12 @@ + + cn.hutool + hutool-core + 5.8.31 + + org.springframework.boot spring-boot-starter-web diff --git a/src/main/java/cn/hezhaohui/threadpool/ThreadPoolApplication.java b/src/main/java/cn/hezhaohui/threadpool/ThreadPoolApplication.java index 92f6f3f..cee0401 100644 --- a/src/main/java/cn/hezhaohui/threadpool/ThreadPoolApplication.java +++ b/src/main/java/cn/hezhaohui/threadpool/ThreadPoolApplication.java @@ -1,11 +1,42 @@ package cn.hezhaohui.threadpool; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import cn.hutool.core.thread.ExecutorBuilder; +import lombok.extern.slf4j.Slf4j; -@SpringBootApplication +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +@Slf4j public class ThreadPoolApplication { public static void main(String[] args) { - SpringApplication.run(ThreadPoolApplication.class, args); + ExecutorBuilder builder = new ExecutorBuilder(); + ThreadPoolExecutor executor = builder + .setCorePoolSize(5) + .setMaxPoolSize(10) + .setKeepAliveTime(0) + .setHandler(new RejectedExecutionHandler(){ + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { + log.info(r.toString()); + } + }) + .setWorkQueue(new ArrayBlockingQueue<>(100)) + .build(); + + for (int i = 1; i <= 1000; i++) { + executor.execute(() -> { + log.info("Hello"); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }); + } + + executor.shutdown(); + } }