博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ExecutorService
阅读量:6435 次
发布时间:2019-06-23

本文共 3152 字,大约阅读时间需要 10 分钟。

. . .ExecutorService

 

 

An that provides methods to manage termination and methods that can produce a for tracking progress of one or more asynchronous tasks.

An ExecutorService can be shut down, which will cause it to reject new tasks. Two different methods are provided for shutting down an ExecutorService. The method will allow previously submitted tasks to execute before terminating, while the method prevents waiting tasks from starting and attempts to stop currently executing tasks. Upon termination, an executor has no tasks actively executing, no tasks awaiting execution, and no new tasks can be submitted. An unused ExecutorService should be shut down to allow reclamation of its resources.

Method submit extends base method by creating and returning a that can be used to cancel execution and/or wait for completion. Methods invokeAny and invokeAll perform the most commonly useful forms of bulk execution, executing a collection of tasks and then waiting for at least one, or all, to complete. (Class can be used to write customized variants of these methods.)

The class provides factory methods for the executor services provided in this package.

Usage Examples

Here is a sketch of a network service in which threads in a thread pool service incoming requests. It uses the preconfigured factory method:
class NetworkService implements Runnable {   private final ServerSocket serverSocket;   private final ExecutorService pool;   public NetworkService(int port, int poolSize)       throws IOException {     serverSocket = new ServerSocket(port);     pool = Executors.newFixedThreadPool(poolSize);   }   public void run() { // run the service     try {       for (;;) {         pool.execute(new Handler(serverSocket.accept()));       }     } catch (IOException ex) {       pool.shutdown();     }   } } class Handler implements Runnable {   private final Socket socket;   Handler(Socket socket) { this.socket = socket; }   public void run() {     // read and service request on socket   } }
The following method shuts down an
ExecutorService in two phases, first by calling
shutdown to reject incoming tasks, and then calling
shutdownNow, if necessary, to cancel any lingering tasks:
void shutdownAndAwaitTermination(ExecutorService pool) {   pool.shutdown(); // Disable new tasks from being submitted   try {     // Wait a while for existing tasks to terminate     if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {       pool.shutdownNow(); // Cancel currently executing tasks       // Wait a while for tasks to respond to being cancelled       if (!pool.awaitTermination(60, TimeUnit.SECONDS))           System.err.println("Pool did not terminate");     }   } catch (InterruptedException ie) {     // (Re-)Cancel if current thread also interrupted     pool.shutdownNow();     // Preserve interrupt status     Thread.currentThread().interrupt();   } }

Memory consistency effects: Actions in a thread prior to the submission of a Runnable or Callable task to an ExecutorService any actions taken by that task, which in turn happen-before the result is retrieved via Future.get().

Since:
1.5
 
 

转载于:https://www.cnblogs.com/xxdfly/p/5635474.html

你可能感兴趣的文章
阿里云上Kubernetes集群联邦
查看>>
我的Git忽略文件
查看>>
洛谷2219:[HAOI2007]修筑绿化带——题解
查看>>
监控webservice信息
查看>>
a标签中href=""的几种用法(转)
查看>>
python
查看>>
ubuntu 常用生产环境部署配置测试调优
查看>>
【JS】//将中文逗号转换为英文逗号
查看>>
在VS2012中实现Ext JS的智能提示太简单了
查看>>
Extnet Direct 提交后台事件文件下载设置
查看>>
邻接矩阵与二叉排序树
查看>>
CSS选择器
查看>>
购物车练习
查看>>
js实现在表格中删除和添加一行
查看>>
SOCKET简单爬虫实现代码和使用方法
查看>>
跨域解决方案汇总
查看>>
In App Purchase
查看>>
js判断对象的类型的四种方式
查看>>
RPC框架的可靠性设计
查看>>
使用自选择创建团队
查看>>