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();
            }
           }
            
            
           
        }
    
    }
  • 相关阅读:
    (中等) HDU 1495 非常可乐,BFS。
    (简单) POJ 1562 Oil Deposits,BFS。
    (简单) POJ 3984 迷宫问题,BFS。
    动态规划(斐波那契系列)---爬楼梯
    回溯---N皇后
    回溯---数独
    回溯---分割字符串使得每个部分都是回文数
    回溯---含有相同元素求子集
    回溯---子集
    回溯---组合求和
  • 原文地址:https://www.cnblogs.com/MagicAsa/p/10938266.html
Copyright © 2011-2022 走看看