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();
            }
           }
            
            
           
        }
    
    }
  • 相关阅读:
    Web scraping tutorials with FMiner
    javascript
    Installing perl and writing your first perl program in Ubuntu
    c++
    sudo apt-get install libfcgi libfcgi-dev
    微信JSApi支付~订单号和微信交易号
    微信JSApi支付~坑和如何填坑
    WebApi系列~安全校验中的防篡改和防复用
    EF架构~CodeFirst自关联表的插入
    实时监控Cat之旅~对请求是否正常结束做监控(分布式的消息树)
  • 原文地址:https://www.cnblogs.com/MagicAsa/p/10938266.html
Copyright © 2011-2022 走看看