关于java中Timer类的一个简单实例

来源:互联网 发布:淘宝美即面膜牛奶白滑 编辑:程序博客网 时间:2024/06/05 09:43

import java.util.Timer;

import java.util.TimerTask;
public class  Tips{
 private int seconds;
 private Timer timer;
 private int count;
 public Tips(int seconds){
  this.seconds=seconds;
  timer=new Timer();
  count=0;
 }
 public void timerTips(){
  timer.schedule(new TimerTask(){
   public void run(){
    tips();
    timerTips();
    if(count==5){
     timer.cancel();
     System.out.println("时间到了!");
    }
   }
   public void tips(){
    count++;
    System.out.println(count+"秒");
    
   }
  },seconds*1000);
 }
 public static void main(String[] args) {
  Tips tip=new Tips(1);
  tip.timerTips();
 }
}
该程序设置为到5秒时结束运行。

原创粉丝点击