1. fixedthreadpool
//创建一个固定大小的线程池,模拟提交 10 个任务到线程池。 import java.util.concurrent.executorservice; import java.util.concurrent.executors; public class fixedthreadpoolexample { public static void main(string[] args) { executorservice fixedthreadpool = executors.newfixedthreadpool(3); // 创建一个具有3个线程的固定线程池 for (int i = 1; i <= 10; i++) { final int task = i; fixedthreadpool.execute(() -> { system.out.println("执行任务 " + task + ",线程:" + thread.currentthread().getname()); }); } fixedthreadpool.shutdown(); } }
特点:创建一个固定大小的线程池,池中始终保持指定数量的线程。
适用场景:适用于固定并发数的任务,比如定量的短期并发任务。
优点:能够有效地控制线程数量,避免资源消耗过多。
缺点:如果所有线程都在执行任务,而新的任务不断提交,可能会造成等待队列过长。
2. cachedthreadpool
//创建一个缓存线程池来处理任务,模拟并发执行 10 个任务 import java.util.concurrent.executorservice; import java.util.concurrent.executors; public class cachedthreadpoolexample { public static void main(string[] args) { executorservice cachedthreadpool = executors.newcachedthreadpool(); for (int i = 1; i <= 10; i++) { final int task = i; cachedthreadpool.execute(() -> { system.out.println("执行任务 " + task + ",线程:" + thread.currentthread().getname()); }); } cachedthreadpool.shutdown(); } }
- 特点:创建一个可以根据需要自动扩展的线程池,当线程空闲 60 秒后会被回收。
- 适用场景:适合执行大量耗时较短的异步任务。
- 优点:线程数量不受限制(受系统资源限制),对于任务短小、并发量大但不稳定的场景效果较好。
- 缺点:如果任务增长过快,会创建大量线程,可能会造成 oom(out of memory)异常。
3. singlethreadexecutor
//创建一个单线程线程池,顺序执行多个任务。 import java.util.concurrent.executorservice; import java.util.concurrent.executors; public class singlethreadexecutorexample { public static void main(string[] args) { executorservice singlethreadexecutor = executors.newsinglethreadexecutor(); for (int i = 1; i <= 5; i++) { final int task = i; singlethreadexecutor.execute(() -> { system.out.println("执行任务 " + task + ",线程:" + thread.currentthread().getname()); }); } singlethreadexecutor.shutdown(); } }
- 特点:创建单线程化的线程池,始终只有一个工作线程。
- 适用场景:适用于需要保证任务顺序执行的场景,避免多线程并发的复杂性。
- 优点:可以保证任务按顺序执行,适合单一任务队列。
- 缺点:性能较低,不适合需要高并发的场景。
4. scheduledthreadpool
//创建一个支持定时和周期性执行任务的线程池,示例任务每隔 2 秒执行一次,共执行 3 次。 import java.util.concurrent.executors; import java.util.concurrent.scheduledexecutorservice; import java.util.concurrent.timeunit; public class scheduledthreadpoolexample { public static void main(string[] args) { scheduledexecutorservice scheduledthreadpool = executors.newscheduledthreadpool(2); // 创建一个有2个线程的定时线程池 scheduledthreadpool.scheduleatfixedrate(() -> { system.out.println("定时任务执行,线程:" + thread.currentthread().getname()); }, 0, 2, timeunit.seconds); // 0秒延迟后开始,每隔2秒执行一次任务 // 程序运行5秒后关闭线程池 try { thread.sleep(5000); } catch (interruptedexception e) { e.printstacktrace(); } scheduledthreadpool.shutdown(); } }
- 特点:创建一个支持定时或周期性任务执行的线程池。
- 适用场景:适合执行定时任务或周期性任务,比如定时器、定时检查等。
- 优点:可以方便地实现周期性任务管理。
- 缺点:对高并发任务的处理能力较弱,通常用于任务量不大的场景。
5. workstealingpool(java 8 引入)
//创建一个基于任务分解的线程池来并行执行多个任务,适合处理需要拆分的小任务。 import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.timeunit; public class workstealingpoolexample { public static void main(string[] args) throws interruptedexception { executorservice workstealingpool = executors.newworkstealingpool(); // 创建默认线程数为cpu核心数的工作窃取线程池 for (int i = 1; i <= 8; i++) { final int task = i; workstealingpool.submit(() -> { system.out.println("执行任务 " + task + ",线程:" + thread.currentthread().getname()); try { timeunit.seconds.sleep(1); } catch (interruptedexception e) { e.printstacktrace(); } }); } // 让主线程等待子任务执行完成 workstealingpool.awaittermination(3, timeunit.seconds); workstealingpool.shutdown(); } }
- 特点:基于
forkjoinpool
实现,适用于大任务拆分成小任务的并行处理。线程数默认为处理器核心数。 - 适用场景:适合处理较为复杂的并行任务,比如分治算法。
- 优点:通过“工作窃取”算法实现任务的动态负载均衡,能够有效提升多核 cpu 的利用率。
- 缺点:由于线程数不固定,可能对资源使用较多,不适合所有应用。
区别总结
线程池类型 | 线程数量控制 | 特点 | 适用场景 |
---|---|---|---|
fixedthreadpool | 固定数量 | 固定线程数,适合稳定的任务并发 | 固定并发任务 |
cachedthreadpool | 自动扩展 | 动态扩展,空闲线程自动回收,适合任务短小但并发量不稳定 | 短期的异步并发任务 |
singlethreadexecutor | 单一线程 | 单线程顺序执行任务,保证顺序 | 顺序执行的任务 |
scheduledthreadpool | 可控核心线程数 | 支持定时或周期性任务 | 定时任务、周期性任务 |
workstealingpool | 默认 cpu 核数 | 基于任务拆分并行处理,提高多核 cpu 利用率 | 并行计算和多任务的分解 |
以上就是java中创建线程池的几种方式以及区别的详细内容,更多关于java创建线程池的资料请关注代码网其它相关文章!
发表评论