• 定时器


    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.Date;
    
    public class UsingTimer{
        static class MyTimerTask extends TimerTask{
            private int taskId ;
            
            public MyTimerTask(int taskId){
                this.taskId = taskId ;
                }
                
            public void run(){
                System.out.println("taskId["+this.taskId+"] at time["+System.currentTimeMillis()+"]");
                }
            }
        public static void main(String[] args){
            Timer timer = new Timer();
            
            MyTimerTask task1 = new MyTimerTask(1);
            timer.schedule(task1, 200);
            
            MyTimerTask task2 = new MyTimerTask(2);
            Date Date = new Date(System.currentTimeMillis()+1000);
            timer.schedule(task2, Date);
            
            MyTimerTask task3 = new MyTimerTask(3);
            timer.schedule(task3, 200, 500);
            
            try{
                Thread.sleep(2000);
            }catch(InterruptedException e){
                e.printStackTrace();
                }
                
            timer.cancel();    
            System.out.println("timer canceled!");
            }
        }

    运行结果:

    G:maul keyboard hread imer>javac UsingTimer.java

    G:maul keyboard hread imer>java UsingTimer
    taskId[1] at time[1535889634267]
    taskId[3] at time[1535889634267]
    taskId[3] at time[1535889634767]
    taskId[2] at time[1535889635067]
    taskId[3] at time[1535889635267]
    taskId[3] at time[1535889635768]
    timer canceled!

    G:maul keyboard hread imer>

  • 相关阅读:
    第六阶段·数据库MySQL及NoSQL实践第1章·章节一MySQL数据库
    小象和老鼠
    好句子啊
    LGTB 与 序列
    最小环
    精灵魔法
    C#委托之我见
    MySQL——优化ORDER BY语句
    MySQL——索引实现原理
    是什么影响了数据库索引选型?
  • 原文地址:https://www.cnblogs.com/celine/p/9575144.html
Copyright © 2020-2023  润新知