更好的资源管理方式

时间:2016-08-15 00:45:54

标签: java

我还是Java新手,并制作了一个计时器,每小时弹出一次,提醒计算机用户起床和移动,基本上在后台运行,直到弹出窗口显示。但是,我决定在main中使用while循环需要大量资源。我已经尝试过Thread.sleep(使用次数不同)的方法,但是当弹出窗口应该关闭时,它无法始终排队。所以,我决定不使用那种方法。此应用程序的资源管理还有其他想法吗?这是所有类的源代码。

主要类别和方法

package houralarm;

public class HourAlarm 
{
    public static void main(String[] args)
    {   
        // TimerLoop Class Object
        TimerLoop timerLoop = new TimerLoop();

        // bool to run the while loop
       Bboolean run = true;


        while(run) 
        {
            if(timerLoop.quitInt == 0)
            {              
                timerLoop.Loop();
            }             
        }
    }
}

获得时间等级

package houralarm;

import java.util.Date;
import java.text.*;

public class GetTime 
{    
    public String ReturnTime()
    {
        // string to hold the date format
        String minutes;
        // Date object
        Date dateObj = new Date();
        // SimpleDateFormat object with the formatting only.         holding the minutes
        SimpleDateFormat dateFormat = new.    SimpleDateFormat("mm");

        // Sets the date to the times variable
        minutes = dateFormat.format(dateObj);

        // Returns the minutes
        return minutes;
    }
}

TIMERLOOP CLASS     包裹houralarm;

import javax.swing.JOptionPane;

public class TimerLoop
{
    // GetTime Class Object
    GetTime getTime = new GetTime();
    WhenAlarm whenAlarm = new WhenAlarm();
    // Holds the GetTime Classes return value from ReturnTime()
    String t = getTime.ReturnTime();
    // Used for the while loop
    boolean quit = false;
    // Used to tell Main() when to quit and start the loop again
    int quitInt = 0;
    String answer = whenAlarm.Answer();   
    public void Loop()
    {   
        while (!quit)
        {   
            // Runs continously to update the time
            t = getTime.ReturnTime();

            if (answer.equals("00") && t.equals("00"))
                whenAlarm.OnHour(answer);
            else if (answer.equals("15") && t.equals("15"))
                whenAlarm.FifteenMinutes(answer);           
            else if (answer.equals("30") && t.equals("30"))
                whenAlarm.ThirtyMinutes(answer);
            else if(answer.equals("45") && t.equals("45"))
                whenAlarm.FortyFiveMinutes(answer);
            else
            {
                t = getTime.ReturnTime();
                break;
            }                          
        }
    }
}

警报等级

package houralarm;

import javax.swing.*;

public class WhenAlarm 
{
    GetTime getTime = new GetTime();
    String t = getTime.ReturnTime();
    boolean check = true;

    public String Answer ()
    {

        String answer = JOptionPane.showInputDialog(null,     "When would you like for this alarm to go off?\n"
            + "Type 15 for every hour on the quarter hour.\n" + "Type 30 for every hour on the half hour.\n" + 
            "Type 45 for every hour on the 3/4 hour.\n" + "Type 00 for every hour on the hour.", "When do you want this alarm to happen?" , JOptionPane.QUESTION_MESSAGE);

       String returnAnswer = answer;
        return returnAnswer;
}

    public void FifteenMinutes(String time)
    {      

       while(check){
       if (time.equals("15"))
       {
           JOptionPane.showMessageDialog(null, "It's Time To  Get Up And Walk Around!", "Hour Timer", JOptionPane.WARNING_MESSAGE);
           t = getTime.ReturnTime();
           break;
        }
       else
        break;  
    }
}

    public void ThirtyMinutes (String time)
    {
        while(check){
       if (time.equals("30"))
       {
           JOptionPane.showMessageDialog(null, "It's Time To Get Up And Walk Around!", "Hour Timer", JOptionPane.WARNING_MESSAGE);
           t = getTime.ReturnTime();
           break;
       }
       else
        break;  
    }
}
    public void FortyFiveMinutes(String time)
    {
       while(check){
           if (time.equals("45"))
           {
           JOptionPane.showMessageDialog(null, "It's Time To Get Up And Walk Around!", "Hour Timer", JOptionPane.WARNING_MESSAGE);
           t = getTime.ReturnTime();
           break;
       }
       else
        break;  
    }
}

public void OnHour(String time)
{
   while(check){
       if (time.equals("00"))
       {
           JOptionPane.showMessageDialog(null, "It's Time To Get Up And Walk Around!", "Hour Timer", JOptionPane.WARNING_MESSAGE);
           t = getTime.ReturnTime();
           break;
       }
       else
        break;  
      }
    }
}

0 个答案:

没有答案