AbstractExecutorService — ExecutorService 실행 메서드 기본 구현

AbstractExecutorService — ExecutorService 실행 메서드 기본 구현

ExecutorService의 실행 메서드에 기본 구현을 제공하는 추상 클래스예요.

출처: Java API Reference

본문

시그니처

public abstract class AbstractExecutorService extends Object
    implements ExecutorService

구현된 인터페이스: AutoCloseable, Executor, ExecutorService.

설명

이 클래스는 ExecutorService 실행 메서드의 기본 구현을 제공해요. submit, invokeAny, invokeAll 메서드를 newTaskFor가 반환하는 RunnableFuture를 사용해 구현하며, 기본값은 이 패키지에 제공된 FutureTask 클래스예요. 예를 들어 submit(Runnable)의 구현은 실행·반환되는 연관 RunnableFuture를 만들어요. 하위 클래스는 newTaskFor 메서드를 오버라이드해 FutureTask가 아닌 RunnableFuture 구현을 반환할 수 있어요.

확장 예제. ThreadPoolExecutor를 커스터마이즈해 기본 FutureTask 대신 CustomTask 클래스를 사용하는 클래스 스케치예요:

public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
  static class CustomTask<V> implements RunnableFuture<V> { ... }
  protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
      return new CustomTask<V>(c);
  }
  protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
      return new CustomTask<V>(r, v);
  }
  // ... add constructors, etc.
}

주요 메서드

  • submit(Callable<T> task) — 변환 값이 있는 작업을 실행하고 해당 작업을 나타내는 Future를 반환해요.
  • submit(Runnable task) — 실행 가능한 작업을 실행하고 해당 작업을 나타내는 Future를 반환해요.
  • invokeAll(Collection<? extends Callable<T>> tasks) — 주어진 작업들을 실행하고, 모두 완료되면 그들의 상태와 결과를 가진 Future 목록을 반환해요.
  • invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) — 주어진 작업들을 실행하고, 모두 완료되거나 시간 제한이 초과될 때 Future 목록을 반환해요.
  • invokeAny(Collection<? extends Callable<T>> tasks) — 주어진 작업들을 실행하고, 하나가 성공적으로 완료되면(예외 없이) 그 결과를 반환해요.

더 알아보기 (Learn more)