我的java 计时器 一运行就会是cpu的利用率达到100%,感觉自己的程序有问题,但又找不到问题所在,高手帮忙!急急!!
原代码如下:
import java.io.*;
import java.util.Timer;
import java.util.TimerTask;
import java.text.SimpleDateFormat;
import java.text.ParsePosition;
import java.util.Date;
public class CountTimer {   
    private final Timer timer = new Timer();
    private final int minutes;
private final String firstTime;
private final String lastTime;
private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public CountTimer(int minutes,String firstTime,String lastTime) { 
       this.minutes = minutes;
       this.firstTime=firstTime;
   this.lastTime=lastTime;
    }
    public void start(){    
   Date fistDate=sdf.parse(firstTime,new ParsePosition(0));
       Date lastDate=sdf.parse(lastTime,new ParsePosition(0));
   MyTask myTask= new MyTask();
       timer.schedule(myTask,fistDate, minutes * 60 * 1000);
   while(true){
       if(new Date().equals(lastDate)){
       timer.cancel();
   }
   }
    } 
static class MyTask extends java.util.TimerTask{
        @Override
        public void run(){
            tcount(); 
        }
    public void tcount(){
    try{
        new Count().test();
}
 catch(Exception e){
      System.out.println(e);
 }
   }
    }
   public static void main(String[] args) { 
       CountTimer countTimer = new CountTimer(1,"2008-11-22 22:04:00","2008-11-22 22:14:00"); 
       countTimer.start();
    }
}
class Count { 
    private static int allcount; 
    public static void savecount() throws IOException { 
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("count.dat")); 
        out.writeObject(allcount); 
        out.close(); 
    } 
    public static int readcount(){
    String s ="";  
try{
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("count.dat")); 
            s = in.readObject().toString();
            in.close();
}catch(Exception e){
    System.out.println(e);
}
return allcount=Integer.valueOf(s); 
    } 
    public static int add() throws IOException,ClassNotFoundException 
    { 
        return allcount++; 
    } 
public static void test()throws IOException,ClassNotFoundException{
        Count.readcount();   
        Count.add(); 
        Count.savecount();
System.out.println(Count.readcount());
}
}

解决方案 »

  1.   

    while(true){
               if(new Date().equals(lastDate)){
                   timer.cancel();
               }
           }
    这样运行肯定CPU是100%了,如果一定要这种方式加个sleep吧:
    while(true){
               if(new Date().equals(lastDate)){
                   timer.cancel();
                   Thread.sleep(n);
               }
           }
      

  2.   


    不用sleep,我看这个程序问题处在这里  if(new Date().equals(lastDate))这个语句的判断,2个时间肯定一直不等,否则就退出了
      

  3.   

    2楼说的不错
    while(true){ 
              if(new Date().equals(lastDate)){ 
                  timer.cancel(); 
              } 
    } 就是这的问题
    为什么一定要用获取时间比较呢你可以获取一次当前时间,然后计算出当前时间和lastTime中间的差值,然后让timer执行相应的时间不久行了
      

  4.   

    两个时间是能够相等的,我觉得问题出在当两个时间相等之后并没有跳出while循环,需要加个break跳出while循环
    while(true){ 
              if(new Date().equals(lastDate)){ 
                  timer.cancel();
                  break;
              } 
          } 
      

  5.   

    问题的确是在这里,但是为什么要加“Thread.sleep(n)“我是要将任务结束。这里休眠有什么作用呢?不理解,能给解释一下吗? 
      

  6.   

    将关闭放到另一个Timer中,不能使用while(true)