File tree Expand file tree Collapse file tree 1 file changed +4
-4
lines changed
Expand file tree Collapse file tree 1 file changed +4
-4
lines changed Original file line number Diff line number Diff line change 1313
1414` Executors ` 返回线程池对象的弊端如下(后文会详细介绍到):
1515
16- - ** ` FixedThreadPool ` 和 ` SingleThreadExecutor ` ** :使用的是无界的 ` LinkedBlockingQueue ` ,任务队列最大长度为 ` Integer.MAX_VALUE ` , 可能堆积大量的请求,从而导致 OOM。
17- - ** ` CachedThreadPool ` ** :使用的是同步队列 ` SynchronousQueue ` , 允许创建的线程数量为 ` Integer.MAX_VALUE ` ,可能会创建大量线程,从而导致 OOM。
18- - ** ` ScheduledThreadPool ` 和 ` SingleThreadScheduledExecutor ` ** : 使用的无界的延迟阻塞队列` DelayedWorkQueue ` ,任务队列最大长度为 ` Integer.MAX_VALUE ` , 可能堆积大量的请求,从而导致 OOM。
16+ - ** ` FixedThreadPool ` 和 ` SingleThreadExecutor ` ** :使用的是有界阻塞队列 ` LinkedBlockingQueue ` ,任务队列的默认长度和最大长度为 ` Integer.MAX_VALUE ` , 可能堆积大量的请求,从而导致 OOM。
17+ - ** ` CachedThreadPool ` ** :使用的是同步队列 ` SynchronousQueue ` , 允许创建的线程数量为 ` Integer.MAX_VALUE ` ,可能会创建大量线程,从而导致 OOM。
18+ - ** ` ScheduledThreadPool ` 和 ` SingleThreadScheduledExecutor ` ** : 使用的无界的延迟阻塞队列` DelayedWorkQueue ` ,任务队列最大长度为 ` Integer.MAX_VALUE ` , 可能堆积大量的请求,从而导致 OOM。
1919
2020说白了就是:** 使用有界队列,控制线程创建数量。**
2121
@@ -227,7 +227,7 @@ try {
227227
228228线程池本身的目的是为了提高任务执行效率,避免因频繁创建和销毁线程而带来的性能开销。如果将耗时任务提交到线程池中执行,可能会导致线程池中的线程被长时间占用,无法及时响应其他任务,甚至会导致线程池崩溃或者程序假死。
229229
230- 因此,在使用线程池时,我们应该尽量避免将耗时任务提交到线程池中执行。对于一些比较耗时的操作,如网络请求、文件读写等,可以采用异步操作的方式来处理 ,以避免阻塞线程池中的线程。
230+ 因此,在使用线程池时,我们应该尽量避免将耗时任务提交到线程池中执行。对于一些比较耗时的操作,如网络请求、文件读写等,可以采用 ` CompletableFuture ` 等其他异步操作的方式来处理 ,以避免阻塞线程池中的线程。
231231
232232## 8、线程池使用的一些小坑
233233
You can’t perform that action at this time.
0 commit comments