JavaTM Platform
Standard Ed. 6

java.util.concurrent
클래스 ExecutorCompletionService<V>

java.lang.Object 
  상위를 확장 java.util.concurrent.ExecutorCompletionService<V>
모든 구현된 인터페이스:
CompletionService <V>


public class ExecutorCompletionService<V>
extends Object
implements CompletionService <V>

태스크의 실행에, 지정된 Executors 를 사용하는 CompletionService 입니다. 이 클래스는, 송신된 태스크가 완료시에 take 를 사용해 액세스 가능한 큐에 배치되도록(듯이) 준비합니다. 이 클래스는 경량이기 (위해)때문에, 태스크 그룹을 처리할 때에 일시적으로 사용할 수 있습니다.

사용예 각각이 어떠한 Result 형을 돌려주는, 특정의 문제에 대응한 소르바셋트를 보관 유지하고 있어, 이것들을 동시에 실행해, null 이외의 값을 돌려주는 결과를 각각 use(Result r) 메소드로 처리하는 경우를 생각합니다. 다음과 같이 기술할 수가 있습니다.

   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);
       }
   }
 
이번은, 예외가 발생한 결과를 무시해, task set의 null 이외의 최초의 결과를 사용하는 경우를 생각합니다. 유효한 최초의 결과를 취득할 수 있으면(자), 다른 태스크는 모두 취소합니다.
   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);
   }
 


생성자 의 개요
ExecutorCompletionService (Executor  executor)
          기본 태스크의 실행용으로 지정된 executor 를 사용해, 완료 큐로서 LinkedBlockingQueue 를 사용해, ExecutorCompletionService 를 작성합니다.
ExecutorCompletionService (Executor  executor, BlockingQueue <Future <V >> completionQueue)
          기본 태스크의 실행용으로 지정된 executor 를 사용해, 완료 큐로서 지정된 큐를 사용해, ExecutorCompletionService 를 작성합니다.
 
메소드의 개요
 Future <V > poll ()
          다음의 완료필 태스크를 나타내는 Future 를 취득해 삭제합니다.
 Future <V > poll (long timeout, TimeUnit  unit)
          다음의 완료필 태스크를 나타내는 Future 를 취득해 삭제합니다.
 Future <V > submit (Callable <V > task)
          값을 돌려주는 실행용 태스크를 송신해, 보류 상태의 태스크 결과를 나타내는 Future 를 돌려줍니다.
 Future <V > submit (Runnable  task, V  result)
          실행용의 Runnable 태스크를 송신해, 그 태스크를 나타내는 Future 를 돌려줍니다.
 Future <V > take ()
          다음의 완료필 태스크를 나타내는 Future 를 취득해 삭제합니다.
 
클래스 java.lang. Object 로부터 상속된 메소드
clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait
 

생성자 의 상세

ExecutorCompletionService

public ExecutorCompletionService(Executor  executor)
기본 태스크의 실행용으로 지정된 executor 를 사용해, 완료 큐로서 LinkedBlockingQueue 를 사용해, ExecutorCompletionService 를 작성합니다.

파라미터:
executor - 사용하는 executor
예외:
NullPointerException - executor 가 null 의 경우

ExecutorCompletionService

public ExecutorCompletionService(Executor  executor,
                                 BlockingQueue <Future <V >> completionQueue)
기본 태스크의 실행용으로 지정된 executor 를 사용해, 완료 큐로서 지정된 큐를 사용해, ExecutorCompletionService 를 작성합니다.

파라미터:
executor - 사용하는 executor
completionQueue - 일반적으로, 이 서비스 전용의 완료 큐로서 사용하는 큐
예외:
NullPointerException - executor 또는 completionQueue 가 null 의 경우
메소드의 상세

submit

public Future <V > submit(Callable <V > task)
인터페이스 CompletionService 의 기술:
값을 돌려주는 실행용 태스크를 송신해, 보류 상태의 태스크 결과를 나타내는 Future 를 돌려줍니다. 완료시에, 이 태스크를 꺼낼까 폴링 할 수 있습니다.

정의:
인터페이스 CompletionService <V > 내의 submit
파라미터:
task - 송신하는 태스크
반환값:
태스크의 보류 완료를 나타내는 Future

submit

public Future <V > submit(Runnable  task,
                        V  result)
인터페이스 CompletionService 의 기술:
실행용의 Runnable 태스크를 송신해, 그 태스크를 나타내는 Future 를 돌려줍니다. 완료시에, 이 태스크를 꺼낼까 폴링 할 수 있습니다.

정의:
인터페이스 CompletionService <V > 내의 submit
파라미터:
task - 송신하는 태스크
result - 정상적으로 완료했을 경우에 돌려주는 결과
반환값:
태스크의 보류 완료를 나타내는 Future. 그 get() 메소드는, 완료시로 지정된 결과치를 돌려준다

take

public Future <V > take()
               throws InterruptedException 
인터페이스 CompletionService 의 기술:
다음의 완료필 태스크를 나타내는 Future 를 취득해 삭제합니다. 아무것도 존재하지 않는 경우는 대기합니다.

정의:
인터페이스 CompletionService <V > 내의 take
반환값:
다음의 완료필 태스크를 나타내는 Future
예외:
InterruptedException - 대기중에 인터럽트가 발생했을 경우

poll

public Future <V > poll()
인터페이스 CompletionService 의 기술:
다음의 완료필 태스크를 나타내는 Future 를 취득해 삭제합니다. 아무것도 존재하지 않는 경우는 null 를 돌려줍니다.

정의:
인터페이스 CompletionService <V > 내의 poll
반환값:
다음의 완료필 태스크를 나타내는 Future. 아무것도 존재하지 않는 경우는 null

poll

public Future <V > poll(long timeout,
                      TimeUnit  unit)
               throws InterruptedException 
인터페이스 CompletionService 의 기술:
다음의 완료필 태스크를 나타내는 Future 를 취득해 삭제합니다. 아무것도 존재하지 않는 경우는, 필요에 따라서 지정된 대기 시간까지 대기합니다.

정의:
인터페이스 CompletionService <V > 내의 poll
파라미터:
timeout - 처리를 중지할 때까지의 대기 시간. 단위는 unit
unit - timeout 파라미터의 해석 방법을 결정하는 TimeUnit
반환값:
다음의 완료필 태스크를 나타내는 Future. 지정된 대기 시간이 경과해도 아무것도 존재하지 않는 경우는 null
예외:
InterruptedException - 대기중에 인터럽트가 발생했을 경우

JavaTM Platform
Standard Ed. 6

버그의 보고와 기능의 요청
한층 더 자세한 API 레퍼런스 및 개발자 문서에 대해서는,Java SE 개발자용 문서를 참조해 주세요. 개발자전용의 상세한 해설, 개념의 개요, 용어의 정의, 버그의 회피책, 및 코드 실례가 포함되어 있습니다.

Copyright 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms . Documentation Redistribution Policy 도 참조해 주세요.