java.util.Timer
or
javax.swing.Timer
javax.swing.Timer用法:
javax.swing.Timer timer = new javax.swing.Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e) {
  System.out.println("hello");
}
});
timer.start();
每隔一秒打印一行hello
---------------------------------------------
java.util.Timer用法如下:
java.util.Timer timer = new Timer(true);
        timer.schedule(new java.util.TimerTask() {
            public void run() {
                    System.out.println("hello");
            }
        }, 0,1000);
每隔1秒打印一行hello,初始化延迟为0秒
使用这几行代码之后,Timer本身会每隔5分钟调用一遍System.out.println("hello");方法,不需要自己启动线程。Timer本身也是多线程同步的,多个线程可以共用一个Timer,不需要外部的同步代码