|
JavaTM Platform Standard Ed. 6 |
|||||||||
전의 클래스 다음의 클래스 | 프레임 있어 프레임 없음 | |||||||||
개요: 상자 | 필드 | 생성자 | 메소드 | 상세: 필드 | 생성자 | 메소드 |
java.lang.Objectjava.util.concurrent.ExecutorCompletionService<V>
public class ExecutorCompletionService<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 |
생성자 의 상세 |
---|
public ExecutorCompletionService(Executor executor)
LinkedBlockingQueue
를 사용해, ExecutorCompletionService 를 작성합니다.
executor
- 사용하는 executor
NullPointerException
- executor 가 null 의 경우public ExecutorCompletionService(Executor executor, BlockingQueue <Future <V >> completionQueue)
executor
- 사용하는 executorcompletionQueue
- 일반적으로, 이 서비스 전용의 완료 큐로서 사용하는 큐
NullPointerException
- executor 또는 completionQueue 가 null 의 경우메소드의 상세 |
---|
public Future <V > submit(Callable <V > task)
CompletionService
의 기술:
CompletionService <V >
내의 submit
task
- 송신하는 태스크
public Future <V > submit(Runnable task, V result)
CompletionService
의 기술:
CompletionService <V >
내의 submit
task
- 송신하는 태스크result
- 정상적으로 완료했을 경우에 돌려주는 결과
public Future <V > take() throws InterruptedException
CompletionService
의 기술:
CompletionService <V >
내의 take
InterruptedException
- 대기중에 인터럽트가 발생했을 경우public Future <V > poll()
CompletionService
의 기술:
CompletionService <V >
내의 poll
public Future <V > poll(long timeout, TimeUnit unit) throws InterruptedException
CompletionService
의 기술:
CompletionService <V >
내의 poll
timeout
- 처리를 중지할 때까지의 대기 시간. 단위는 unitunit
- timeout 파라미터의 해석 방법을 결정하는 TimeUnit
InterruptedException
- 대기중에 인터럽트가 발생했을 경우
|
JavaTM Platform Standard Ed. 6 |
|||||||||
전의 클래스 다음의 클래스 | 프레임 있어 프레임 없음 | |||||||||
개요: 상자 | 필드 | 생성자 | 메소드 | 상세: 필드 | 생성자 | 메소드 |
Copyright 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms . Documentation Redistribution Policy 도 참조해 주세요.