PROGRAMING/Timer

[JAVA] ScheduledExecutorService Timer 적용(자동 리플레쉬)

Popix 2020. 11. 6. 17:54

public static void main(String[] args) {

System.out.println("##### AutoTimer Start #####");

Runnable runnable = new Runnable() {
    public void run() {

        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        Calendar cal = Calendar.getInstance();
        String today = formatter.format(cal.getTime());
        Timestamp ts = Timestamp.valueOf(today); //java.sql

        System.out.println("##### AutoTimer ReTry Time : " + ts + " #####");

    }
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); //    ScheduledExecutorService and Executors : java.util.concurrent 
service.scheduleAtFixedRate(runnable, 0, 30, TimeUnit.SECONDS); // TimeUnit : java.util.concurrent / 0 : 시작 딜레이(바로시작), 30 : 30초에 한번 실행

}