zoukankan      html  css  js  c++  java
  • java定时器demo

    package cn.threadtest.thread;
    
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TraditionalTimerTest {
    
        /**
         * @param args
         */
        
        //每隔2秒,4秒炸方法1
        // static int count = 0;     
        
        //每隔2秒,4秒炸方法2
        static class MyTimerTask1 extends TimerTask {
             
              
                public void run() {
                    
                    System.out.println("bombing!");
                    new  Timer().schedule(new MyTimerTask2(), 2000);
    
               }
         }
          
          
          static class MyTimerTask2 extends TimerTask {
                 
              
                public void run() {
    
                    System.out.println("bombing!");
                    new  Timer().schedule(new MyTimerTask1(), 4000);
    
              }
        }
        //每隔2秒,4秒炸方法2结束
           
        
        public static void main(String[] args) {
            
            //1第10秒和第13秒炸
    //        new Timer().schedule(new TimerTask() {
    //            
    //            public void run() {
    //                // TODO Auto-generated method stub
    //                System.out.println("bombing!");
    //            }
    //        }, 10000,3000);
            
            
    //          new Timer().schedule(new TimerTask() {
    //                
    //                public void run() {
    //                    // TODO Auto-generated method stub
    //                    System.out.println("bombing!");
    //                    new  Timer().schedule(/*new TimerTask(){
    //                        public void run() {
    //                            System.out.println("bombing2!");
    //                        };
    //                    }*/this, 2000);
    //                }
    //            }, 2000);
              
    //            
            
             //每隔2秒炸开始
    //          class MyTimerTask extends TimerTask {
    //                
    //                public void run() {
    //                    // TODO Auto-generated method stub
    //                    System.out.println("bombing!");
    //                    new  Timer().schedule(/*new TimerTask(){
    //                        public void run() {
    //                            System.out.println("bombing2!");
    //                        };
    //                    }*/new MyTimerTask(), 2000);
    //
    //             }
    //      }
            //new  Timer().schedule(new MyTimerTask(), 2000);
             //每隔2秒炸结束
    //          
    
             //每隔2秒,4秒炸开始方法1
    //          class MyTimerTask extends TimerTask {
    //             
    //              
    //                public void run() {
    //                    
    //                     count=(count+1)%2;
    //                     
    //                    // TODO Auto-generated method stub
    //                    System.out.println("bombing!");
    //                    new  Timer().schedule(/*new TimerTask(){
    //                        public void run() {
    //                            System.out.println("bombing2!");
    //                        };
    //                    }*/new MyTimerTask(), 2000+2000*count);
    //
    //           }
    //    }
            //  new  Timer().schedule(new MyTimerTask(), 2000);
         //每隔2秒,4秒炸方法1结束    
            
            //每隔2秒,4秒炸方法2
             
              
              
             new  Timer().schedule(new MyTimerTask2(), 2000);
            
       
            //每隔2秒,4秒炸方法2结束
              
           while(true){
               System.out.println(new Date().getSeconds());
               try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           }
            
            
           
        }
    
    }
  • 相关阅读:
    二分图 洛谷P2055 [ZJOI2009]假期的宿舍
    并查集 洛谷P1640 [SCOI2010]连续攻击游戏
    贪心 洛谷P2870 Best Cow Line, Gold
    贪心 NOIP2013 积木大赛
    快速幂 NOIP2013 转圈游戏
    倍增LCA NOIP2013 货车运输
    树形DP 洛谷P2014 选课
    KMP UVA1328 Period
    动态规划入门 BZOJ 1270 雷涛的小猫
    KMP POJ 2752Seek the Name, Seek the Fame
  • 原文地址:https://www.cnblogs.com/MagicAsa/p/10938266.html
Copyright © 2011-2022 走看看