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)