Programming > Java

[Java] ScheduledExecutorService Interface

#multitasking#다중화#쓰레드#Thread#비동기#Async

ScheduledExecutorService 의 실행 메소드

메소드 매개변수 설명
schedule Runnable task, long delay, TimeUnit unit delay 이후 작업실행 1번
Callable task, long delay, TimeUnit unit delay 이후 작업실행 1번(결과 반환)
scheduleAtFixedRate Runnable task, long delay, long period, TimeUnit unit delay 이후 period간격으로 반복실행
scheduleWithFixedDelay Runnable task, long delay, long period, TimeUnit unit delay 이후 시작, 작업종료 후 period이후 실행을 반복

Scheduled Thread Pool 생성

ScheduledExecutorService ses = Executors.newScheduledThreadPool(5);

1) schedule (Runnable) 한번 실행하고 종료

ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);

// Task 생성
Runnable runnable = () -> System.out.printf("%12s : %s\n","Running", LocalTime.now());

// Task 실행
System.out.printf("%12s : %s\n","___START___",LocalTime.now());
ses.schedule(runnable, 2, TimeUnit.SECONDS); // delay

// Pool 종료
ses.shutdown();
System.out.println("____END____");

___START___ : 15:39:42.638632

____END____
     Running : 15:39:44.651793500

2) schedule (Callable) 한번 실행하고 종료 (결과값 받음)

ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);

// Task 생성
Callable<String> callable = () -> "Running : " + LocalTime.now();

// Task 실행
System.out.println("_START_ : " + LocalTime.now());
ScheduledFuture<String> future = ses.schedule(callable, 2, TimeUnit.SECONDS);

// 결과를 기다렸다 종료함
String result = future.get();
System.out.println(result);
ses.shutdownNow();

System.out.println("__END__");

_START_ : 15:46:02.157365300
Running : 15:46:04.171846900
__END__

3) scheduleAtFixedRate (작업 시작시간 기준 일정 간격으로 실행)

ScheduledExecutorService ses = Executors.newScheduledThreadPool(3);

// Task 생성
Runnable runnable = () -> {
           System.out.println("ST Running : " + LocalTime.now());
           try {
                     TimeUnit.SECONDS.sleep(2); // 작업 2초
           } catch (InterruptedException e) {}
           System.out.println("ED Running : " + LocalTime.now());
};

// Task 실행
System.out.println("_START_ : " + LocalTime.now());

// 2초 기다렸다 실행, 매 3초마다 작업실행한다.
ses.scheduleAtFixedRate(runnable, 2, 3, TimeUnit.SECONDS);

_START_ : 15:53:24.069280500
ST Running : 15:53:26.075375700
ED Running : 15:53:28.079598600
ST Running : 15:53:29.079543600
ED Running : 15:53:31.091023400
ST Running : 15:53:32.083785600
ED Running : 15:53:34.090887600
ST Running : 15:53:35.082352200
ED Running : 15:53:37.083144100
....
....
....

4) scheduleWithFixedDelay(작업종료시점에서 일정대기 후 재작업)

ScheduledExecutorService ses = Executors.newScheduledThreadPool(3);

// Task 생성
Runnable runnable = () -> {
           System.out.println("ST Running : " + LocalTime.now());
           try {
                     TimeUnit.SECONDS.sleep(2); // 작업 2초
           } catch (InterruptedException e) {}
           System.out.println("ED Running : " + LocalTime.now());
};

// Task 실행
System.out.println("_START_ : " + LocalTime.now());

// 2초기다렸다 실행, 작업이 끝난시점부터 3초대기 후 재실행을 반복
ses.scheduleWithFixedDelay(runnable, 2, 3, TimeUnit.SECONDS);

_START_ : 15:58:56.448389400
ST Running : 15:58:58.452994500
ED Running : 15:59:00.459878
ST Running : 15:59:03.467490700
ED Running : 15:59:05.471717100
ST Running : 15:59:08.471984400
ED Running : 15:59:10.475067900
ST Running : 15:59:13.482427200
ED Running : 15:59:15.491274
....
....
....