爪哇 5 has introduced new framework called 执行器框架
for managing 线s.We have already seen before how to create a 线.
If you have noted, we need to create an object of 线 class using new 线(runnableObject)
, so we need to create 线 object for each task.Imagine a situation where you have thousands of task to be 执行d and if you create each 线 for thousands of tasks, you may get performance overheads as creation and maintenance of each 线 is also an overhead. 执行者 framework will solve the problem. In executor framework, you can create specified number of 线s and reuse them to 执行 more tasks once it completes its current task.
执行者 framework
简化了创建多线程应用程序的设计并管理了线程的生命周期。程序员不必自己创建或管理线程。’executor框架的最大优势。
执行器框架有一些重要的类或接口。
执行者
此接口用于提交新任务。它具有一种称为"execute".
1 2 3 4 5 |
上市 接口 执行者 { 虚空 执行(可运行 命令); } |
执行器服务
它是Executor的子界面。此界面为执行者提供了管理任务生命周期的方法。
例如,它提供了用于关闭执行程序的方法。
ScheduledExecutorService
它是执行程序服务的子接口,它提供用于以固定间隔或具有初始延迟计划任务的方法。
执行者
此类提供了用于创建线程池的工厂方法。
执行器的重要工厂方法是:
线程池执行器
爪哇 执行器框架教程
- 爪哇 线程池执行器示例
- 使用Callable和Future的Java 执行器服务示例
- 爪哇 newFixedThreadPool示例
- 爪哇 newCachedThreadPool示例
- 爪哇 newSingleThreadExecutor示例
- 爪哇 ScheduledThreadExecutor示例
- 爪哇 未来任务示例
- 爪哇 执行者CompletionService示例