Executors可以帮助我们创建线程池,下面给大家介绍常见的5大Executors创建线程池的方式@mikechen
什么是Executors
Executors
是 Java 标准库中提供的一个工具类,用于创建和管理线程池。
Executors类创建线程池
Executors
类提供了一系列静态工厂方法,用于创建不同类型的线程池。
主要包含如下4大类,Executors
类返回的线程池实例都实现了 ExecutorService
接口。
1.newFixedThreadPool(int nThreads)
这个工厂方法创建一个固定大小的线程池,其中包含固定数量的线程。
如下所示:
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class FixedThreadPoolExample { public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(4); for (int i = 0; i < 10; i++) { final int taskNumber = i; executorService.submit(() -> { System.out.println("Task " + taskNumber + " executed by thread " + Thread.currentThread().getName()); }); } executorService.shutdown(); } }
如果提交的任务数超过线程池的大小,任务将会在队列中等待执行。
2.newCachedThreadPool()
创建一个可以自动调整大小的线程池,根据任务数量动态地创建和销毁线程。
如下所示:
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CachedThreadPoolExample { public static void main(String[] args) { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { final int taskNumber = i; executorService.submit(() -> { System.out.println("Task " + taskNumber + " executed by thread " + Thread.currentThread().getName()); }); } executorService.shutdown(); } }
适用于任务数不确定或任务持续时间较短的场景。
3.newSingleThreadExecutor()
创建一个只有一个线程的线程池,适用于需要保证任务顺序执行的场景。
如下所示:
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SingleThreadExecutorExample { public static void main(String[] args) { ExecutorService executorService = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10; i++) { final int taskNumber = i; executorService.submit(() -> { System.out.println("Task " + taskNumber + " executed by thread " + Thread.currentThread().getName()); }); } executorService.shutdown(); } }
4.newScheduledThreadPool(int corePoolSize)
创建一个可以执行定时任务的线程池,可以指定核心线程数。
如下所示:
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 scheduledExecutorService = Executors.newScheduledThreadPool(2); scheduledExecutorService.scheduleAtFixedRate(() -> { System.out.println("Scheduled task executed by thread " + Thread.currentThread().getName()); }, 0, 1, TimeUnit.SECONDS); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } scheduledExecutorService.shutdown(); } }
5.newWorkStealingPool(int parallelism)
创建一个工作窃取线程池,适用于可以将任务分成多个子任务并并行执行的场景。
如下所示:
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class WorkStealingPoolExample { public static void main(String[] args) { ExecutorService executorService = Executors.newWorkStealingPool(); for (int i = 0; i < 10; i++) { final int taskNumber = i; executorService.submit(() -> { System.out.println("Task " + taskNumber + " executed by thread " + Thread.currentThread().getName()); }); } executorService.shutdown(); } }
需要注意的是,虽然使用 Executors
可以方便地创建线程池,但在实际开发中,要根据业务需求和性能特点,选择合适的线程池类型和参数配置。
陈睿mikechen
十余年BAT架构经验,资深技术专家,就职于阿里、淘宝、百度等一线互联网大厂。
关注「mikechen」公众号,获取更多技术干货!
后台回复【面试】即可获取《史上最全阿里Java面试题总结》,后台回复【架构】,即可获取《阿里架构师进阶专题全部合集》