java Timer TimerTask

1,Timer

Timer的实质上就是一个多线程,从它的类中可以看出:
Java代码
   1. private TimerThread thread = new TimerThread(queue); 


它适用于与时间相关的一些操作,多长时间后运行某个动作,间隔运行某个动作。如:时钟程序我们要每一秒中就刷新一下我们的指针,如,模拟心脏的跳动,Timer都是不错的选择。
 
2,Timer的线程设置成后台线程
Java代码

   1. public class Time { 
   2.     private final Timer timer = new Timer(true);  
   3.  
   4.     public void start() { 
   5.         timer.schedule(new TimerTask() { 
   6.             public void run() { 
   7.                 System.out.println("Your egg is ready!");  
   8.             } 
   9.         }, 1000, 1000);  
  10.     } 
  11.  
  12.     public static void main(String[] args) { 
  13.         Time eggTimer = new Time();  
  14.         eggTimer.start(); 
  15.         try { 
  16.             Thread.sleep(5000); 
  17.         } catch (InterruptedException e) { 
  18.             e.printStackTrace(); 
  19.         } 
  20.     } 
  21. } 

public class Time {
    private final Timer timer = new Timer(true);

    public void start() {
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println("Your egg is ready!");
            }
        }, 1000, 1000);
    }

    public static void main(String[] args) {
        Time eggTimer = new Time();
        eggTimer.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 实现了在1秒钟后,每隔一秒钟后运行一次TimerTask。这个Timer设置成的后台线程,在主线程退出后自动退出,一般的我觉得Timer 都设置成后台的比较好,前段时间我就发现我写的程序退出了,怎么还有javaw.exe在任务管理器中没有退出啊,还发现我的程序运行了很多次后,在任务管理器中的javaw.exe越来越多,我的机器也就越来越慢了,噢,肯定我的程序,还没有完全推出,结果就找到了一个Timer没有退出,后来我就把我程序的所有的Timer都改后台了。Timer一般都是完成某个任务,如果没有了前台线程,它本来就没有存在的意义了,我程序中是利用的Timer去检测文件的改动,然后通知前台程序文件变了。
3,Timer运行一段时间,被cancel
Java代码

   1. public class Time { 
   2.     private final static Timer timer = new Timer();  
   3.  
   4.     public void start() { 
   5.         timer.schedule(new TimerTask() { 
   6.             public void run() { 
   7.                 System.out.println("Your egg is ready!");  
   8.             } 
   9.         }, 1000, 1000);  
  10.     } 
  11.  
  12.     public static void main(String[] args) { 
  13.         Time eggTimer = new Time();  
  14.         eggTimer.start(); 
  15.         try { 
  16.             Thread.sleep(5000); 
  17.             timer.cancel(); 
  18.         } catch (InterruptedException e) { 
  19.             e.printStackTrace(); 
  20.         } 
  21.     } 
  22. } 

public class Time {
    private final static Timer timer = new Timer();

    public void start() {
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println("Your egg is ready!");
            }
        }, 1000, 1000);
    }

    public static void main(String[] args) {
        Time eggTimer = new Time();
        eggTimer.start();
        try {
            Thread.sleep(5000);
            timer.cancel();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
4,Timer运行5次后,被cancel
Java代码

   1. public class Time { 
   2.     private final static Timer timer = new Timer();  
   3.  
   4.     public void start() { 
   5.         timer.schedule(new TimerTask() { 
   6.             private int count = 5; 
   7.             public void run() { 
   8.                 System.out.println("Your egg is ready!"); 
   9.                 if(count--==0) 
  10.                     timer.cancel(); 
  11.             } 
  12.         }, 1000, 1000);  
  13.     } 
  14.  
  15.     public static void main(String[] args) { 
  16.         Time eggTimer = new Time();  
  17.         eggTimer.start(); 
  18.     } 
  19. } 


转自:http://xmind.javaeye.com/blog/718699

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值