티스토리 뷰
어플리케이션에서 실행할 작업들의 시간을 관리하는 것이다. 스케줄링은 어플리케이션의 효율성과 사용자 경험을 개선하는데 매우 중요한 역할을 합니다.
자바에서는 Timer, TimerTask, ScheduledExecutorService 등의 클래스를 사용하여 스케줄링을 구현할 수 있다.
Timer는 한 번이나 여러 번 예약된 작업을 실행할 수 있지만, 스레드의 안정성 문제가 있어 종종 사용되지 않는다.
TimerTask는 Timer에 의해 실행될 작업을 정의하는 클래스이다.
ScheduledExecutorService는 ExecutorService 인터페이스의 확장으로, 스케줄링 기능이 추가된 클래스이다.
ScheduledExecutorService의 장점은 스케줄링 작업의 안정성 및 스케줄링 작업의 취소 등의 기능을 제공하며, Timer와 비교했을 때 더 높은 안정성을 제공한다.
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorExample {
public static void main(String[] args) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime());
int initialDelay = 0;
int period = 1;
executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);
}
}
ScheduledExecutorService 객체를 생성하고, 새로운 스케줄링 작업을 추가하는 scheduleAtFixedRate 메소드를 사용한다.
이 메소드는 첫 번째 인자로 실행할 Runnable 객체, 두 번째 인자로 스케줄링 작업 시작까지의 시간(초), 세 번째 인자로 주기적으로 실행할 시간 간격(초), 네 번째 인자로 시간 단위를 지정한다. 이 예제에서는 스케줄링 작업을 즉시 시작하고(initialDelay = 0), 1초마다 반복하여 실행한다.
'Back-end > JAVA' 카테고리의 다른 글
Lock - Spring Scheduler (0) | 2023.02.02 |
---|---|
Spring Scheduler (0) | 2023.02.02 |
Lock - 다중서버 (0) | 2023.01.31 |
etcd (0) | 2023.01.31 |
Lock - StampedLock (0) | 2023.01.30 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 클라우드 컴퓨팅
- aws공부
- aws기초
- aws개발공부
- amazon web services
- 클라우드 컴퓨팅과 아마존 웹 서비스
- aws공부순서
- cloud computing
- AWS
- aws시작
- 클라우드 컴퓨팅(Cloud computing)
- 아마존 웹 서비스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함