先看一个Executor接口,该接口只有一个方法:void execute(Runnable command),用于在未来某个时刻提交一个command,这个command可以被提交到一个新的线程,或者一个线程池,或者在调用线程中。

ExecutorService接口继承了Executor接口。主要是增加了shutDown、shutDownNow、invokeAll、invokeAny和submit方法。

  • invokeAny(Collection<? extends Callable<T>> tasks)
    提交所有任务,返回某个已完成的任务的结果 <T> T,有相应带超时版本。
  • invokeAll(Collection<? extends Callable<T>> tasks)
    提交所有任务,所有任务完成后,返回所有的结果 <T>List<Future<T>>,有相应带超时版本。
  • shutdown()
    执行器不再接受新的任务。
  • List<Runnable> shutDownNow()
    尝试结束所有正在执行的任务。
  • submit
    用于将任务提交给ExecutorService,会返回Future对象,用于查询执行状态。

下面是ExecutorService的源码:

  1. public interface ExecutorService extends Executor {
  2. /**
  3. * Initiates an orderly shutdown in which previously submitted
  4. * tasks are executed, but no new tasks will be accepted.
  5. * Invocation has no additional effect if already shut down.
  6. */
  7. void shutdown();
  8.  
  9. /**
  10. * Attempts to stop all actively executing tasks, halts the
  11. * processing of waiting tasks, and returns a list of the tasks
  12. * that were awaiting execution.
  13. */
  14. List<Runnable> shutdownNow();
  15.  
  16. /**
  17. * Returns <tt>true</tt> if this executor has been shut down.
  18. */
  19. boolean isShutdown();
  20.  
  21. /**
  22. * Returns <tt>true</tt> if all tasks have completed following shut down.
  23. * Note that <tt>isTerminated</tt> is never <tt>true</tt> unless
  24. * either <tt>shutdown</tt> or <tt>shutdownNow</tt> was called first.
  25. */
  26. boolean isTerminated();
  27.  
  28. /**
  29. * Blocks until all tasks have completed execution after a shutdown
  30. * request, or the timeout occurs, or the current thread is
  31. * interrupted, whichever happens first.
  32. */
  33. boolean awaitTermination(long timeout, TimeUnit unit)
  34. throws InterruptedException;
  35.  
  36. /**
  37. * Submits a value-returning task for execution and returns a
  38. * Future representing the pending results of the task. The
  39. * Future's <tt>get</tt> method will return the task's result upon
  40. * successful completion.
  41. */
  42. <T> Future<T> submit(Callable<T> task);
  43.  
  44. /**
  45. * Submits a Runnable task for execution and returns a Future
  46. * representing that task. The Future's <tt>get</tt> method will
  47. * return the given result upon successful completion.
  48. */
  49. <T> Future<T> submit(Runnable task, T result);
  50.  
  51. /**
  52. * Submits a Runnable task for execution and returns a Future
  53. * representing that task. The Future's <tt>get</tt> method will
  54. * return <tt>null</tt> upon <em>successful</em> completion.
  55. */
  56. Future<?> submit(Runnable task);
  57.  
  58. /**
  59. * Executes the given tasks, returning a list of Futures holding
  60. * their status and results when all complete.
  61. */
  62. <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
  63. throws InterruptedException;
  64.  
  65. /**
  66. * Executes the given tasks, returning a list of Futures holding
  67. * their status and results
  68. * when all complete or the timeout expires, whichever happens first.
  69. * {@link Future#isDone} is <tt>true</tt> for each
  70. * element of the returned list.
  71. * Upon return, tasks that have not completed are cancelled.
  72. */
  73. <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
  74. long timeout, TimeUnit unit)
  75. throws InterruptedException;
  76.  
  77. /**
  78. * Executes the given tasks, returning the result
  79. * of one that has completed successfully (i.e., without throwing
  80. * an exception), if any do. Upon normal or exceptional return,
  81. * tasks that have not completed are cancelled.
  82. * The results of this method are undefined if the given
  83. * collection is modified while this operation is in progress.
  84. */
  85. <T> T invokeAny(Collection<? extends Callable<T>> tasks)
  86. throws InterruptedException, ExecutionException;
  87.  
  88. /**
  89. * Executes the given tasks, returning the result
  90. * of one that has completed successfully (i.e., without throwing
  91. * an exception), if any do before the given timeout elapses.
  92. * Upon normal or exceptional return, tasks that have not
  93. * completed are cancelled.
  94. * The results of this method are undefined if the given
  95. * collection is modified while this operation is in progress.
  96. */
  97. <T> T invokeAny(Collection<? extends Callable<T>> tasks,
  98. long timeout, TimeUnit unit)
  99. throws InterruptedException, ExecutionException, TimeoutException;
  100. }

ExecutorService 接口的更多相关文章

  1. ExecutorService接口概要

    ExecutorService接口继承于Executor接口,主要提供以下额外功能: 管理终结 产生Future对象,用于跟踪一个或多个任务的进度.   ExecutorService可以被shut ...

  2. 聊聊高并发(三十九)解析java.util.concurrent各个组件(十五) 理解ExecutorService接口的设计

    上一篇讲了Executor接口的设计,目的是将任务的运行和任务的提交解耦.能够隐藏任务的运行策略.这篇说说ExecutorService接口.它扩展了Executor接口,对Executor的生命周期 ...

  3. JUC之Executor,ExecutorService接口,AbstractExecutorService类

    java多线程的Executor中定义了一个execut方法,ExecutorService接口继承了Executor接口,并进行了功能的扩展组合,定义了shutdown,shutdownNow,su ...

  4. java中ExecutorService接口

    一.声明 public interface ExecutorService extends Executor 位于java.util.concurrent包下 所有超级接口:Executor 所有已知 ...

  5. Executor框架(二)Executor 与 ExecutorService两个基本接口

    一.Executor 接口简介 Executor接口是Executor框架的一个最基本的接口,Executor框架的大部分类都直接或间接地实现了此接口. 只有一个方法 void execute(Run ...

  6. java线程池相关接口Executor和ExecutorService

    在线程池的api中,Executor接口是最上层的接口,内部只有一个方法.如下: public interface Executor { void execute(Runnable command); ...

  7. java多线程之Executor 与 ExecutorService两个基本接口

    一.Executor 接口简介 Executor接口是Executor框架的一个最基本的接口,Executor框架的大部分类都直接或间接地实现了此接口. 只有一个方法 void execute(Run ...

  8. Android线程管理之ExecutorService线程池

    前言: 上篇学习了线程Thread的使用,今天来学习一下线程池ExecutorService. 线程管理相关文章地址: Android线程管理之Thread使用总结 Android线程管理之Execu ...

  9. ExecutorService中submit()和execute()的区别

    在使用java.util.concurrent下关于线程池一些类的时候,相信很多人和我一样,总是分不清submit()和execute()的区别,今天从源码方面分析总结一下. 通常,我们通过Execu ...

随机推荐

  1. js获取上传文件内容(未完待续)

    js 获取上传文件的字节数及内容 <div> 上传文件 : <input type="file" name = "file" id = &qu ...

  2. ajax请求,请求头是provisional are shown。请求未发送出去

    问题: ajax请求,请求没成功.ajax请求没有发送出去. 查看network,看到请求头处:Provisional headers are shown. 原因: 搜索了一下,网上说了几个原因. 1 ...

  3. HP LoadRunner 11 破解及license

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  4. C#-设置窗体在显示器居中显示

    在窗体的属性中查看:StartPosition属性,该属性的设置中有一个"CenterScreen"的选择项,该项就是设置窗体局中显示的.

  5. 使用Zipalign工具优化Android APK应用记录

    生成的Android应用APK文件最好进行优化,因为APK包的本质是一个zip压缩文档,经过优化能使包内未压缩的数据有序的排列,从而减少应用程序运行时的内存消耗.我们可以使用Zipalign工具进行A ...

  6. WebService 设计总结

    接触过非常多电商的WebService,有种一看就蛋疼的设计,今天要从这个反例说一说 WebService 的设计. [WebMethod] public string QueryOrderDetai ...

  7. [Effective C++ --024]若所有参数皆需类型转换,请为此采用non-member函数

    引言 假设我们有这样的类: class A{ public: A(, ) {}; int num() const; int den() const; const A operator* (const ...

  8. iOS的崩溃和编译错误

    1. Command /bin/sh failed with exit code 127 这是因为mogenerator找不到路径,stackoverflow给出的答案是: If /usr/local ...

  9. C#基础篇--文件(流)

    1:Path类是专门用来操作文件路径的(Path类是静态类):当然用字符串的处理办法也能实现.  string str = @"C:\Users\成才\Desktop\Hashtable.t ...

  10. VSPackge插件系列:简单文本编辑器的实现

    相比其它开发环境,VS的好用就不用多说了,尽管VS很人性化,但是针对具体的我们想实现的功能时,会力不从心,也许会有很多现成的插件,但是作为一名程序员,我还是喜欢自己去写一些东西,因为这样能随心所欲的想 ...