import java.util.Timer; import java.util.TimerTask; class Task extends TimerTask { private volatile int taskNum = 1; public void run() { System.out.println(taskNum); taskNum++; } } public class UtilTimer { public static void main(String[] args) { Timer timer2 = new Timer(); timer2.scheduleAtFixedRate(new Task(), 0, 1000); // タスクの実行間隔は1000ミリ秒 try { Thread.sleep(5000); } catch (InterruptedException ignore) { } timer2.cancel(); } }