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();
        }
       }
        
        
       
    }

}
原文地址:https://www.cnblogs.com/MagicAsa/p/10938266.html