🔄Update: 使用 Hutool 创建线程池
This commit is contained in:
@@ -23,6 +23,12 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>5.8.31</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user