传统定时器

/**
 * 
 * @描述: 传统定时器 .
 * @作者: Wnj .
 * @创建时间: 2017年5月15日 .
 * @版本: 1.0 .
 */
public class TraditionalTimerTest {
    
    private static int count = 0;
    
    public static void main(String[] args) {
        /*        
         *     宿主
         *      new Timer().schedule(new TimerTask() {
                    
                    @Override
                    public void run() {
                        System.out.println("bombing!");
                        
                    }
                    //10秒后开始,间隔3秒执行一次
                }, 10000,3000);*/
        
        class MyTimerTask extends TimerTask {
            
            @Override
            public void run() {
                count = (count + 1) % 2;
                System.out.println("bombing!");
                new Timer().schedule(/*new TimerTask() {
                                     
                                     @Override
                                     public void run() {
                                     System.out.println("bombing!");
                                     }
                                     }*/new MyTimerTask(), 2000 + 2000 * count);
            }
        }
        
        new Timer().schedule(new MyTimerTask(), 2000);
        
        while (true) {
            System.out.println(new Date().getSeconds());
            try {
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

 

posted @ 2017-05-25 09:59  superGG  阅读(132)  评论(0编辑  收藏  举报