Development

ScheduledThreadPoolExecutor - 주기적인 실행

¯¯\_(ツ)_/¯·2018년 2월 1일·조회 2,406
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {

	public static void main(String[] args) {
		int sleepSec = 10;

		final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

		final ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);

		exec.scheduleAtFixedRate(new Runnable() {
			public void run() {
				try {
					Calendar cal = Calendar.getInstance();
					System.out.println(sdf.format(cal.getTime()));

				} catch (Exception e) {
					e.printStackTrace();
					exec.shutdown();
				}
			}
		}, 0, sleepSec, TimeUnit.SECONDS);
	}
}

댓글 0

로그인 후 댓글을 남길 수 있습니다.

아직 댓글이 없습니다.