
package java.util.concurrent;
public interface Executor {
void execute(Runnable command);
}
Executor는 실행하는 객체가 구현해야할 인터페이스입니다. 이 인터페이스는 작업을 제공 하는 코드와 작업을 실행하는 매커니즘 사이의 커플링을 제거해 줍니다
package java.util.concurrent;
public interface ExecutorService extends Executor {
void shutdown();
List<Runnable> shutdownNow();
boolean isShutdown();
boolean isTerminated();
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
<T> Future<T> submit(Callable<T> task);
<T> Future<T> submit(Runnable task, T result);
Future<?> submit(Runnable task);
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException;
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException;
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException;
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
Executor의 라이플 사이클을 관리할 수 있는 기능이 정의된 인터페이스입니다. Runnable 뿐만 아니라 Callable도 작업을 사용할 수 있습니다.
public interface ScheduledExecutorService extends ExecutorService {
public ScheduledFuture<?> schedule(Runnable command,
long delay, TimeUnit unit);
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
long delay, TimeUnit unit);
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
}
// Runnable
val executor = Executors.newScheduledThreadPool(2)
val runnable = Runnable { println("Runnable task : ${LocalTime.now()}") }
val delay = 3
executor.schedule(runnable, delay, TimeUnit.SECONDS)
// Callable
val executor = Executors.newScheduledThreadPool(2)
val callable = Callable { "Callable task : ${LocalTime.now()}" }
val delay = 3
println("Scheduled task: ${LocalTime.now()}")
val future = executor.schedule(callable, delay, TimeUnit.SECONDS)
val result: String = future.get()
println(result)
간단히만 알아본 내용들 다음은 자세한 내용들을 다룰 예정.