- 浏览: 984354 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
Executor接口的定义:http://donald-draper.iteye.com/blog/2365625
ExecutorService接口定义:http://donald-draper.iteye.com/blog/2365738
Future接口定义:http://donald-draper.iteye.com/blog/2365798
FutureTask解析:http://donald-draper.iteye.com/blog/2365980
CompletionService接口定义:http://donald-draper.iteye.com/blog/2366239
总结:
ExecutorCompletionService内部关联一个执行器AbstractExecutorService和
一个阻塞的任务完成队列,默认为LinkedBlockingQueue。当提交任务,则包装成QueueingFuture,QueueingFuture
扩展了FutureTask,重写done方法,即在任务执行结束时,添加任务执行结果到完成队列。
而take,poll,超时poll直接委托给完成队列。
ExecutorService接口定义:http://donald-draper.iteye.com/blog/2365738
Future接口定义:http://donald-draper.iteye.com/blog/2365798
FutureTask解析:http://donald-draper.iteye.com/blog/2365980
CompletionService接口定义:http://donald-draper.iteye.com/blog/2366239
package java.util.concurrent; /** * A {@link CompletionService} that uses a supplied {@link Executor} * to execute tasks. This class arranges that submitted tasks are, * upon completion, placed on a queue accessible using {@code take}. * The class is lightweight enough to be suitable for transient use * when processing groups of tasks. * ExecutorCompletionService是执行器的补充或者说是辅助,用于执行任务。 任务提交到ExecutorCompletionService执行后,如果任务执行完,则添加到完成 队列,我们可以通过take,取回完成任务的结果。ExecutorCompletionService 是处理集合任务的一个轻量级的实现。 * <p> * * <b>Usage Examples.</b> * 实例: * Suppose you have a set of solvers for a certain problem, each * returning a value of some type {@code Result}, and would like to * run them concurrently, processing the results of each of them that * return a non-null value, in some method {@code use(Result r)}. You * could write this as: * 假设将一个确定的问题,分成n个部分,这n个部分可以并发执行,每个部分返回一个非null,结果 在其他一些方法中,使用这些结果。 * <pre> {@code * void solve(Executor e, * Collection<Callable<Result>> solvers) * throws InterruptedException, ExecutionException { * CompletionService<Result> ecs * = new ExecutorCompletionService<Result>(e); * for (Callable<Result> s : solvers) * ecs.submit(s); * int n = solvers.size(); * for (int i = 0; i < n; ++i) { * Result r = ecs.take().get(); * if (r != null) * use(r); * } * }}</pre> * * Suppose instead that you would like to use the first non-null result * of the set of tasks, ignoring any that encounter exceptions, * and cancelling all other tasks when the first one is ready: * 假设我们仅想用任务集合中第一个完成任务的结果,并忽略其他任务遇到的异常, 当第一个完成任务的结果可用时,取消其他任务。 * <pre> {@code * void solve(Executor e, * Collection<Callable<Result>> solvers) * throws InterruptedException { * CompletionService<Result> ecs * = new ExecutorCompletionService<Result>(e); * int n = solvers.size(); * List<Future<Result>> futures * = new ArrayList<Future<Result>>(n); * Result result = null; * try { * for (Callable<Result> s : solvers) * futures.add(ecs.submit(s)); * for (int i = 0; i < n; ++i) { * try { * Result r = ecs.take().get(); * if (r != null) { * result = r; * break; * } * } catch (ExecutionException ignore) {} * } * } * finally { * for (Future<Result> f : futures) * f.cancel(true); * } * * if (result != null) * use(result); * }}</pre> */ public class ExecutorCompletionService<V> implements CompletionService<V> { private final Executor executor;//执行器 private final AbstractExecutorService aes;//执行器服务 private final BlockingQueue<Future<V>> completionQueue;//任务完成队列 /** * FutureTask extension to enqueue upon completion 扩展FutureTask的队列完成任务 */ private class QueueingFuture extends FutureTask<Void> { QueueingFuture(RunnableFuture<V> task) { super(task, null); this.task = task; } //这个是关键,在FutureTask那篇文章中,我们有讲,及当任务完成时调用done方法, //done方法为抽象方法,待子类扩展 protected void done() { //当任务任务执行结束时,添加到完成队列 completionQueue.add(task); } private final Future<V> task; } //根据Callable,创建 private RunnableFuture<V> newTaskFor(Callable<V> task) { if (aes == null) return new FutureTask<V>(task); else return aes.newTaskFor(task); } private RunnableFuture<V> newTaskFor(Runnable task, V result) { if (aes == null) return new FutureTask<V>(task, result); else return aes.newTaskFor(task, result); } /** * Creates an ExecutorCompletionService using the supplied * executor for base task execution and a * {@link LinkedBlockingQueue} as a completion queue. * 根据执行器构建ExecutorCompletionService,完成队列默认为LinkedBlockingQueue * @param executor the executor to use * @throws NullPointerException if executor is {@code null} */ public ExecutorCompletionService(Executor executor) { if (executor == null) throw new NullPointerException(); this.executor = executor; this.aes = (executor instanceof AbstractExecutorService) ? (AbstractExecutorService) executor : null; //将完成任务放在LinkedBlockingQueue中 this.completionQueue = new LinkedBlockingQueue<Future<V>>(); } /** * Creates an ExecutorCompletionService using the supplied * executor for base task execution and the supplied queue as its * completion queue. * 根据执行器和完成队列构建ExecutorCompletionService * @param executor the executor to use * @param completionQueue the queue to use as the completion queue * normally one dedicated for use by this service. This * queue is treated as unbounded -- failed attempted * {@code Queue.add} operations for completed taskes cause * them not to be retrievable. * @throws NullPointerException if executor or completionQueue are {@code null} */ public ExecutorCompletionService(Executor executor, BlockingQueue<Future<V>> completionQueue) { if (executor == null || completionQueue == null) throw new NullPointerException(); this.executor = executor; this.aes = (executor instanceof AbstractExecutorService) ? (AbstractExecutorService) executor : null; this.completionQueue = completionQueue; } //提交执行Callable任务 public Future<V> submit(Callable<V> task) { if (task == null) throw new NullPointerException(); RunnableFuture<V> f = newTaskFor(task); executor.execute(new QueueingFuture(f)); return f; } //提交执行Runnable任务 public Future<V> submit(Runnable task, V result) { if (task == null) throw new NullPointerException(); RunnableFuture<V> f = newTaskFor(task, result); executor.execute(new QueueingFuture(f)); return f; } //从完成队列take完成任务的结果,没有则阻塞,直到有任务完成 public Future<V> take() throws InterruptedException { return completionQueue.take(); } //从完成队列获取完成任务的结果,没有则返回null public Future<V> poll() { return completionQueue.poll(); } //从完成队列获取完成任务的结果,没有则超时等待,如果超时等待期间还没有完成任务,返回为null public Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException { return completionQueue.poll(timeout, unit); } }
总结:
ExecutorCompletionService内部关联一个执行器AbstractExecutorService和
一个阻塞的任务完成队列,默认为LinkedBlockingQueue。当提交任务,则包装成QueueingFuture,QueueingFuture
扩展了FutureTask,重写done方法,即在任务执行结束时,添加任务执行结果到完成队列。
而take,poll,超时poll直接委托给完成队列。
发表评论
-
Executors解析
2017-04-07 14:38 1255ThreadPoolExecutor解析一(核心线程池数量、线 ... -
ScheduledThreadPoolExecutor解析三(关闭线程池)
2017-04-06 20:52 4455ScheduledThreadPoolExecutor解析一( ... -
ScheduledThreadPoolExecutor解析二(任务调度)
2017-04-06 12:56 2127ScheduledThreadPoolExecutor解析一( ... -
ScheduledThreadPoolExecutor解析一(调度任务,任务队列)
2017-04-04 22:59 4992Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析四(线程池关闭)
2017-04-03 23:02 9112Executor接口的定义:http: ... -
ThreadPoolExecutor解析三(线程池执行提交任务)
2017-04-03 12:06 6087Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析二(线程工厂、工作线程,拒绝策略等)
2017-04-01 17:12 3041Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析一(核心线程池数量、线程池状态等)
2017-03-31 22:01 20520Executor接口的定义:http://donald-dra ... -
ScheduledExecutorService接口定义
2017-03-29 12:53 1510Executor接口的定义:http://donald-dra ... -
AbstractExecutorService解析
2017-03-29 08:27 1080Executor接口的定义:http: ... -
CompletionService接口定义
2017-03-28 12:39 1068Executor接口的定义:http://donald-dra ... -
FutureTask解析
2017-03-27 12:59 1329package java.util.concurrent; ... -
Future接口定义
2017-03-26 09:40 1198/* * Written by Doug Lea with ... -
ExecutorService接口定义
2017-03-25 22:14 1162Executor接口的定义:http://donald-dra ... -
Executor接口的定义
2017-03-24 23:24 1677package java.util.concurrent; ... -
简单测试线程池拒绝执行任务策略
2017-03-24 22:37 2031线程池多余任务的拒绝执行策略有四中,分别是直接丢弃任务Disc ... -
JAVA集合类简单综述
2017-03-23 22:51 925Queue接口定义:http://donald-draper. ... -
DelayQueue解析
2017-03-23 11:00 1737Queue接口定义:http://donald-draper. ... -
SynchronousQueue解析下-TransferQueue
2017-03-22 22:20 2140Queue接口定义:http://donald-draper. ... -
SynchronousQueue解析上-TransferStack
2017-03-21 22:08 3059Queue接口定义:http://donald-draper. ...
相关推荐
在使用`ExecutorCompletionService`时,我们需要创建一个`ExecutorService`实例,然后将这个`ExecutorService`传递给`ExecutorCompletionService`的构造函数。接着,我们可以向`ExecutorCompletionService`提交任务...
Callable,Future的使用方式,里面使用了三种使用方式分别是FutureTask,ExecutorService,ExecutorCompletionService
本篇学习笔记将深入解析Java线程池的框架、结构、原理以及相关源码,帮助读者全面理解线程池的工作机制。 1. 线程池模块结构 线程池框架分为多层结构,其中包括核心实现类、辅助类和接口等组件。例如,`sun.nio.ch....
包括阻塞队列、阻塞栈、ExecutorService、Future、ExecutorCompletionService、死锁、join、重入锁、读写锁、多线程抢票、信号量、signal/await、ThreadLocal等的实例。
`ExecutorCompletionService`是`java.util.concurrent`包提供的一个类,它结合了`ExecutorService`和`BlockingQueue`的功能,用于管理和获取已完成的任务结果。 综上所述,Java中的多线程并发机制非常强大,不仅...
`ExecutorCompletionService`结合了`ExecutorService`和`BlockingQueue`的功能,主要用于管理和监控异步任务的执行结果。 #### 三、锁机制 在多线程编程中,锁是确保数据完整性和一致性的重要手段。`java.util....
在示例中,创建了一个ExecutorCompletionService实例,它继承自CompletionService并且使用ExecutorService作为底层的执行器。提交任务的方式与ExecutorService类似,但获取结果时,不再直接从列表中获取Future,而是...
4. `CompletionService`:可能是`ExecutorCompletionService`,它结合了`ExecutorService`和`BlockingQueue`的功能。我们可以使用`CompletionService.take()`方法获取下一个已完成的任务的结果,而不必等待所有任务...
7. **ExecutorCompletionService** - 一个基于`ExecutorService`的增强版服务,用于管理一组异步任务的执行和结果收集。 8. **ScheduledExecutorService** - 支持定时及周期性任务执行的接口,如`...
CompletionService<Object> ecs = new ExecutorCompletionService(executor); for (int i = 0; i ; i++) { final Integer t = data[i]; ecs.submit(new Callable() { public Object call() { try { Thread....
- `ExecutorCompletionService`:用于管理一组异步任务,等待任务完成并获取结果。 - `ForkJoinPool`和`RecursiveTask`/`RecursiveAction`:基于工作窃取算法的并行计算框架。 8. **线程中断和守护线程**: 使用...
`ThreadPoolExecutor`的`submit()`返回`Future`对象,而`ExecutorCompletionService`的`submit()`除了返回`Future`,还支持批量处理结果。 当线程池中的线程抛出异常时,如果使用`submit()`,异常会被捕获并封装在`...
- 并发工具类:Semaphore、CyclicBarrier、CountDownLatch、ExecutorCompletionService等。 - Future和Callable接口:理解异步计算,以及如何获取结果。 通过深入学习这些知识点,Java开发者可以更好地准备面试,...
ExecutorCompletionService类是其实现,它利用线程池执行任务,并帮助开发者获取已经完成的任务结果。 Runnable和Callable是两种任务类型。Runnable是任务的一个简单的执行对象,没有返回值。而Callable接口类似于...
CompletionService<PartETag> completionService = new ExecutorCompletionService(executor); for (int i = 1; i ; i++) { UploadPartResponse response = s3Client.uploadPart(uploadRequestBuilder.part...
10. **TrackingExecutor任务跟踪**:为了确保任务的正常结束,可以使用`ExecutorCompletionService`来跟踪任务的完成情况,并在必要时取消未完成的任务。 11. **处理异常的线程终止**:线程异常终止时,需要正确...
`ExecutorCompletionService`用于批量处理完成的任务,提高效率。 8. **框架源码分析**: 分析如`Akka`、`Quasar`或`Disruptor`等并发框架的源码,可以深入理解如何在Java中构建高效的并发系统,学习其设计思想和...
5. **异步编程**:Java 8引入了CompletableFuture和ExecutorCompletionService等工具,使得开发者能更高效地处理异步任务,提高系统性能。 6. **Web框架**:为了简化开发,项目可能使用Spring Boot或Struts等Web...
14. **并发编程**:深入研究并发工具类(如CountDownLatch, CyclicBarrier, Semaphore, ExecutorCompletionService等),以及并发容器(如ConcurrentHashMap, CopyOnWriteArrayList等)。 15. **垃圾回收与内存管理...