Java thread summary (xiv)

1, in the asynchronous task progress, a common scenario is that the main thread submit multiple asynchronous tasks, and then hope to have completed the task on the results, and one by one according to the order processing task is completed, for this scenario, Java and provides contract a convenient method using a CompletionService, which is an interface, the implementation of which is ExecutorCompletionService.

2, and as ExecutorService, may submit a CompletionService asynchronous task, it is different, it can complete the task according to the order of acquisition result, which specifically is defined as:

1
2
3
4
5
6
7
public interface <> {
Future<V> submit(Callable<V> task);
Future<V> submit(Runnable task, V result);
Future<V> take() throws InterruptedException;
Future<V> poll();
Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;
}

submit method and ExecutorService is the same, and more take the poll method, which is to get the next results of completed tasks, take () will block waiting, poll () returns immediately if the task is not completed, returns null, parameters with time poll method will wait for up to a limited time.

2, CompletionService main implementation class is ExecutorCompletionService, it relies on a Executor do the actual job submission, queuing and while he is responsible for processing results. Its constructor method has two:

1
2
public ExecutorCompletionService(Executor executor)
public ExecutorCompletionService(Executor executor, BlockingQueue<Future<V>> completionQueue)

Executor requires at least one parameter, it can provide a BlockingQueue parameters used to complete the task queue, not supplied, the internal ExecutorCompletionService creates a LinkedBlockingQueue.

3, ExecutorCompletionService is how to make the result of an orderly handle it?
Because it has an extra queue after each task is completed, the results will be representative of the Future into the team. In FutureTask, the task is completed, whether it is normal completion, abnormal termination or cancellation, we will call finishCompletion method, and the method will call a done method protected void done() { }for implementing the method is empty, but it is a protected method, subclasses override this method. Internal class QueueingFuture ExecutorCompletionService in the override method.

In ExecutorCompletionService, the task is not an ordinary type submitted FutureTask, but a subclass QueueingFuture

Large columns   Java thread summary (xiv) >. 1 
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
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;
}
----------------------------

private final BlockingQueue<Future<V>> completionQueue;

private class QueueingFuture extends FutureTask<Void> {
QueueingFuture(RunnableFuture<V> task) {
super(task, null);
this.task = task;
}

protected void done() { completionQueue.add(task); }
private final Future<V> task;
}
---------------------------

And take / poll method is ExecutorCompletionService acquisition result from the queue:

1
2
3
public Future<V> take() throws InterruptedException {
return completionQueue.take();
}

4, to achieve invokeAny of AbstractExecutorService, and on the use of ExecutorCompletionService, its basic idea is that after the submission of the task, get results take method, after obtaining the first valid result, cancel all other tasks.

5, CompletionService it through an additional result queue, to facilitate handling a plurality of asynchronous tasks results.

Reference blog

Java programming logic - convenient CompletionService

Guess you like

Origin www.cnblogs.com/lijianming180/p/12014364.html