2013-02-10 76 views
0

請你可以檢查下面的代碼,這是數字時鐘沒有util.concurrent包,日曆使用睡眠,通知,等待。代碼正確執行,請提出任何更好的方法。非常感謝數字時鐘與多線程只有睡眠,等待和通知

/** 
* 
*/ 
package com.nagihome.clock; 

/** 
* @author Nagi 
* 
*/ 
public class ThreadAlarmClock { 

    private static Integer hours = 23; 
    private static Integer minutes = 59; 
    private static Integer seconds = 55; 
    private static Integer minSecLastNum = 60; 
    private static Integer hour24LastNum = 24; 
    private static String COLON = ":"; 
    private static Boolean isMinUpdatable = false; 
    private static Boolean isHourUpdatable = false; 
    final static Object lock = new Object(); 

    public void updateSeconds() throws InterruptedException { 

     synchronized (lock) { 
      while (true) { 
       if (isMinUpdatable || isHourUpdatable) { 
        lock.wait(); 
       } else { 
        Thread.sleep(1000); 
        seconds++; 
        if (seconds.equals(minSecLastNum)) { 
         seconds = 0; 
         isMinUpdatable = true; 
         lock.notifyAll(); 
        } else { 
         displayTime(); 
        } 
       } 
      } 
     } 
    } 

    public void updateMinutes() throws InterruptedException { 

     synchronized (lock) { 
      while (true) { 
       if (!isMinUpdatable) { 
        lock.wait(); 
       } else if (isMinUpdatable) { 
        minutes++; 
        isMinUpdatable = false; 
        if (minutes.equals(minSecLastNum)) { 
         minutes = 0; 
         isHourUpdatable = true; 
        } else { 
         displayTime(); 
        } 
       } 

       lock.notifyAll(); 
      } 
     } 
    } 

    public void updateHours() throws InterruptedException { 

     synchronized (lock) { 
      while (true) { 
       if (!isHourUpdatable) { 
        lock.wait(); 
       } else if (isHourUpdatable) { 
        hours++; 
        isHourUpdatable = false; 
        if (hours.equals(hour24LastNum)) { 
         hours = 0; 
        } 

        displayTime(); 
       } 

       lock.notifyAll(); 
      } 
     } 
    } 

    public void displayTime() { 
     System.out.println(hours + COLON + minutes + COLON + seconds); 
    } 

} 


/** 
* 
*/ 
package com.nagihome.clock; 

/** 
* @author Nagi 
* 
*/ 
public class SecondsThread implements Runnable { 

    private ThreadAlarmClock clock; 

    public SecondsThread(ThreadAlarmClock clock) { 
     this.clock = clock; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see java.lang.Runnable#run() 
    */ 
    @Override 
    public void run() { 
     try { 
      clock.updateSeconds(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 


/** 
* 
*/ 
package com.nagihome.clock; 

/** 
* @author Nagi 
* 
*/ 
public class MinutesThread implements Runnable { 

    private ThreadAlarmClock clock; 

    public MinutesThread(ThreadAlarmClock clock) { 
     this.clock = clock; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see java.lang.Runnable#run() 
    */ 
    @Override 
    public void run() { 
     try { 
      clock.updateMinutes(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 


/** 
* 
*/ 
package com.nagihome.clock; 

/** 
* @author Nagi 
* 
*/ 
public class HoursThread implements Runnable { 

    private ThreadAlarmClock clock; 

    public HoursThread(ThreadAlarmClock clock) { 
     this.clock = clock; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see java.lang.Runnable#run() 
    */ 
    @Override 
    public void run() { 
     try { 
      clock.updateHours(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 


/** 
* 
*/ 
package com.nagihome.main; 

import com.nagihome.clock.HoursThread; 
import com.nagihome.clock.MinutesThread; 
import com.nagihome.clock.SecondsThread; 
import com.nagihome.clock.ThreadAlarmClock; 

/** 
* @author Nagi 
* 
*/ 
public class Main { 

    /** 
    * @param args 
    * @throws InterruptedException 
    */ 
    public static void main(String[] args) throws InterruptedException { 
     ThreadAlarmClock clock = new ThreadAlarmClock(); 

     new Thread(new SecondsThread(clock)).start(); 
     new Thread(new MinutesThread(clock)).start(); 
     new Thread(new HoursThread(clock)).start();   
    } 

} 

回答

0

建議:

  • 內部使用毫秒來計算,而不是秒++
  • 睡眠(1000)不exatcly入睡1000個米利斯,也可以是990' 或1110這樣喚醒後檢查系統時間(或nanoTime),並計算最後一次喚醒時間的差值。例如,如果你發現你睡了1010毫秒,那麼下次你只睡990毫秒。