package shutdownr;
import java.util.*;
public class Main 
{    public static void main(String[] args)
    {
       Date time1_set=new Date();
       Date time2_set=new Date();
       Date time_now=new Date();
       time_now.getTime();
       time1_set.setHours(23);
       time1_set.setMinutes(30);
       time1_set.setSeconds(00);
       time2_set.setHours(4);
       time2_set.setMinutes(59);
       time2_set.setSeconds(59);
     while(true)
        {
            if(time1_set.compareTo(time_now)==-1&&time2_set.compareTo(time_now)==-1)
            {
                try
                {
                    Runtime.getRuntime().exec("shutdown -s -t 1");
                }
                catch(Exception e)
                {
                    System.out.println("error");
                }
            }
        }
    
      
       
      }
       
  }
       
    写了一个定时关机程序,但是资源占用非常高,cpu占用率50%,内存占用9900k多。
期待高手帮忙改进下代码,减少点资源的使用啊   
       
       

解决方案 »

  1.   

    用while(true),不耗资源才怪Timer定时器就可以了
      

  2.   

    Timer还做不到我想要的功能。
    我不是要在一定时间内关机,而是要系统到了特定时间就关机。所以需要程序不断的访问系统时间,与设定时间进行对比。
    有什么好办法
      

  3.   

    我晕
    1.你这样能关机吗?
      就如:
       while(true)
    {
      if(1 == 2)
        System.out.println("来输出我啊");
    }
    你说能有结果吗
    2.能占资源吗?一直用计算机作者循环,计算机的速度是多少
    3.程序时间比的不好,好多方法都还是过时了。
    下面一个例子:
       
      

  4.   

    [code]
    Calendar cal = new GregorianCalendar(); //指定关机时间
          cal.set(Calendar.HOUR_OF_DAY, 12);
          cal.set(Calendar.MINUTE, 25);
          cal.set(Calendar.SECOND, 25);
          //每天在12:25:25秒关机 ,以秒来计算
          while(true)
          {
           Calendar now = new GregorianCalendar(); //当前时间
           if(now.get(Calendar.HOUR_OF_DAY) == cal.get(Calendar.HOUR_OF_DAY)) //首先比较小时
           {
           if(now.get(Calendar.MINUTE) == cal.get(Calendar.MINUTE)) //比较分钟
           {
           if(now.get(Calendar.SECOND) == cal.get(Calendar.SECOND)) //比较秒
           {
           try 
                         { 
                             Runtime.getRuntime().exec("shutdown -s -t 1"); 
                         } 
                         catch(Exception e) 
                         { 
                             System.out.println("error"); 
                         }         }
           }
           }
           
           try {
    Thread.currentThread().sleep(1000); //让当前线程谁一秒,应为是以秒计算的
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
          }
    [/code]
      

  5.   

    怎么没发成功,加了[code][/code]
    Calendar cal = new GregorianCalendar(); //指定关机时间
          cal.set(Calendar.HOUR_OF_DAY, 12);
          cal.set(Calendar.MINUTE, 25);
          cal.set(Calendar.SECOND, 25);
          //每天在12:25:25秒关机 ,以秒来计算
          while(true)
          {
           Calendar now = new GregorianCalendar(); //当前时间
           if(now.get(Calendar.HOUR_OF_DAY) == cal.get(Calendar.HOUR_OF_DAY)) //首先比较小时
           {
           if(now.get(Calendar.MINUTE) == cal.get(Calendar.MINUTE)) //比较分钟
           {
           if(now.get(Calendar.SECOND) == cal.get(Calendar.SECOND)) //比较秒
           {
           try 
                         { 
                             Runtime.getRuntime().exec("shutdown -s -t 1"); 
                         } 
                         catch(Exception e) 
                         { 
                             System.out.println("error"); 
                         }         }
           }
           }
           
           try {
    Thread.currentThread().sleep(1000); //让当前线程谁一秒,应为是以秒计算的
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
          }
      

  6.   

      用Timer可以吧
      Timer里有个参数是延迟多少时间执行某个任务 你把这个参数设为(特定时间-当前时间) 不就行了
      

  7.   

      我觉得不应该把电脑时间错误这样的特殊情况加进去
      如果考虑这种情况 那思路就只能是随时对比系统时间 这样的方法避免不了while(true)的使用 肯定耗费资源
      如果非要考虑的话 只能建两个线程了 其中一个线程对比时间 这样性能会好一点
      总之不能while(true)
      

  8.   

      对了  Timer的schedule方法中有一种是带日期的参数 你把一个Date传进去就能实现到某个时间执行任务了