본문으로 건너뛰기

Java 1초마다 반복, 실행

어떤 내용을 1초마다 반복, 실행하는 방법입니다.

1. Thread.sleep()

Thread.sleep()은 인자로 전달된 milliseconds 만큼 지연을 시킵니다.

아래와 같이 for문에서 sleep()을 사용하여 1초 간격으로 어떤 내용을 반복시킬 수 있습니다.

import java.time.LocalDateTime;

public class Example {

public static void main(String[] args) {

for (int i = 0; i < 10; i++) {
// do something
System.out.println("Repeat something a second: " + LocalDateTime.now());

// sleep 1 sec
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Output:

Repeat something a second: 2022-12-18T06:47:05.931674862
Repeat something a second: 2022-12-18T06:47:06.939187953
Repeat something a second: 2022-12-18T06:47:07.939633919
Repeat something a second: 2022-12-18T06:47:08.940085243
Repeat something a second: 2022-12-18T06:47:09.940596444
Repeat something a second: 2022-12-18T06:47:10.941064544
Repeat something a second: 2022-12-18T06:47:11.941521652
Repeat something a second: 2022-12-18T06:47:12.941997104
Repeat something a second: 2022-12-18T06:47:13.942430146
Repeat something a second: 2022-12-18T06:47:14.942996023

2. TimeUnit.SECONDS.sleep()

TimeUnit.SECONDS.sleep(n)는 n초만큼 지연시킵니다.

아래와 같이 for문에서 1초 간격으로 어떤 내용을 실행시킬 수 있습니다.

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

public class Example {

public static void main(String[] args) {

for (int i = 0; i < 10; i++) {
// do something
System.out.println("Repeat something a second: " + LocalDateTime.now());

// sleep 1 sec
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Output:

Repeat something a second: 2022-12-18T06:48:27.660873910
Repeat something a second: 2022-12-18T06:48:28.667687578
Repeat something a second: 2022-12-18T06:48:29.668084259
Repeat something a second: 2022-12-18T06:48:30.668541468
Repeat something a second: 2022-12-18T06:48:31.669024824
Repeat something a second: 2022-12-18T06:48:32.669485696
Repeat something a second: 2022-12-18T06:48:33.669843649
Repeat something a second: 2022-12-18T06:48:34.670223852
Repeat something a second: 2022-12-18T06:48:35.670655551
Repeat something a second: 2022-12-18T06:48:36.671162759

3. Timer

Timer를 이용하여 어떤 내용을 주기적으로 반복 시킬 수 있습니다.

Timer.scheduleAtFixedRate(TimerTask, delay, period)는 TimerTask의 작업 내용을 period 간격으로 실행시킵니다. delay는 첫번째 작업을 실행할 때 지연시키는 시간입니다.

Timer.cancel()로 주기적인 작업을 종료시킬 수 있습니다. 일정시간이 지난 뒤에 호출하시면 종료되며, cancel()을 호출하지 않으면 무한히 반복 실행됩니다.

import java.time.LocalDateTime;
import java.util.Timer;
import java.util.TimerTask;

public class Example {

public static void main(String[] args) {
TimerTask task = new TimerTask() {
public void run() {
System.out.println("Repeat something a second: " + LocalDateTime.now());
}
};
Timer timer = new Timer("Timer");
long delay = 0;
long period = 1000;
timer.scheduleAtFixedRate(task, delay, period);

// sleep 10 seconds
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("Cancel the timer");
timer.cancel();
}
}

Output:

Repeat something a second: 2022-12-18T06:53:50.734644869
Repeat something a second: 2022-12-18T06:53:51.714507059
Repeat something a second: 2022-12-18T06:53:52.714876200
Repeat something a second: 2022-12-18T06:53:53.714251911
Repeat something a second: 2022-12-18T06:53:54.714715381
Repeat something a second: 2022-12-18T06:53:55.715040314
Repeat something a second: 2022-12-18T06:53:56.714356582
Repeat something a second: 2022-12-18T06:53:57.714837762
Repeat something a second: 2022-12-18T06:53:58.714232157
Repeat something a second: 2022-12-18T06:53:59.714480444
Cancel the timer