import java.util.Timer;
import java.util.TimerTask;

public class Timer {
    ....
    public void schedule(TimerTask task, long delay) {
        ....
    }
    ....
}

public abstract class TimerTask implements Runnable {
    ...
}

1. 使用匿名内部类

new Timer().schedule(new TimeTask(){
    public void run() {
        System.out.println("Time is up.");
        System.exit(1);
    }
}, 5000);
    // 5 秒钟之后执行控制台输出“Time is up”,程序退出;
posted on 2017-08-27 22:58  未雨愁眸  阅读(742)  评论(0编辑  收藏  举报