美文网首页
Java并发 | Timer类

Java并发 | Timer类

作者: icebreakeros | 来源:发表于2019-08-08 21:08 被阅读0次

    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)
    

    相关文章

      网友评论

          本文标题:Java并发 | Timer类

          本文链接:https://www.haomeiwen.com/subject/mbifjctx.html