Java并发 | Timer类

2019-08-08  本文已影响0人  icebreakeros

Timer类

Timer类的主要作用就是设置计划任务,但封装任务的类却是TimerTask

class MyTask extends TimerTask {
    @Override
    public void run() {
        System.out.println("task executor: " + new Date());
    }
}

public class Run {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("current time: " + new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 10);
        Timer timer = new Timer();
        timer.schedule(new MyTask(), calendar.getTime(), 4000);
    }
}

schedule方法

schedule(TimerTask task, long delay)
schedule(TimerTask task, Date time)
schedule(TimerTask task, long delay, long period)
schedule(TimerTask task, Date firstTime, long period)
scheduleAtFixedRate(TimerTask task, long delay, long period)
scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
上一篇下一篇

猜你喜欢

热点阅读