Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 75dd3daef8 | |||
| c76c902319 |
@@ -1 +1 @@
|
|||||||
> 注意:Java ExecutorService 使用的默认等待队列是无限大小的 `LinkedBlockingQueue`,容易 OOM,不推荐使用
|
https://doc.hutool.cn/pages/ExecutorBuilder
|
||||||
@@ -23,6 +23,12 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-core</artifactId>
|
||||||
|
<version>5.8.31</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
|||||||
@@ -1,34 +1,42 @@
|
|||||||
package cn.hezhaohui.threadpool;
|
package cn.hezhaohui.threadpool;
|
||||||
|
|
||||||
|
import cn.hutool.core.thread.ExecutorBuilder;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.RejectedExecutionHandler;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class ThreadPoolApplication {
|
public class ThreadPoolApplication {
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) {
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
ExecutorBuilder builder = new ExecutorBuilder();
|
||||||
for (int i = 0; i < 100000000; i++) {
|
ThreadPoolExecutor executor = builder
|
||||||
executorService.execute(() -> {
|
.setCorePoolSize(5)
|
||||||
StringBuilder builder = new StringBuilder();
|
.setMaxPoolSize(10)
|
||||||
int random = new Random().nextInt(1000);
|
.setKeepAliveTime(0)
|
||||||
for (int j = 1; j < random; j++) {
|
.setHandler(new RejectedExecutionHandler(){
|
||||||
builder.append(j);
|
@Override
|
||||||
|
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
|
||||||
|
log.info(r.toString());
|
||||||
}
|
}
|
||||||
try {
|
})
|
||||||
TimeUnit.HOURS.sleep(1);
|
.setWorkQueue(new ArrayBlockingQueue<>(100))
|
||||||
} catch (InterruptedException e) {
|
.build();
|
||||||
log.error("[Error]");
|
|
||||||
}
|
|
||||||
log.info(builder.toString());
|
|
||||||
|
|
||||||
|
for (int i = 1; i <= 1000; i++) {
|
||||||
|
executor.execute(() -> {
|
||||||
|
log.info("Hello");
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
executorService.shutdown();
|
executor.shutdown();
|
||||||
executorService.awaitTermination(1, TimeUnit.HOURS);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user