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(); + } }