public class EggTimer { PRivate final Timer timer = new Timer(); private final int minutes;
public EggTimer(int minutes) { this.minutes = minutes; }
public void start() { timer.schedule(new TimerTask() { public void run() { playSound(); timer.cancel(); } private void playSound() { System.out.println("Your egg is ready!"); // Start a new thread to play a sound... } }, minutes * 60 * 1000); }
public static void main(String[] args) { EggTimer eggTimer = new EggTimer(2); eggTimer.start(); }
private final Scheduler scheduler = new Scheduler(); private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS"); private final int hourOfDay, minute, second;
public AlarmClock(int hourOfDay, int minute, int second) { this.hourOfDay = hourOfDay; this.minute = minute; this.second = second; }
public void start() { scheduler.schedule(new SchedulerTask() { public void run() { soundAlarm(); } private void soundAlarm() { System.out.println("Wake up! " + "It′s " + dateFormat.format(new Date())); // Start a new thread to sound an alarm... } }, new DailyIterator(hourOfDay, minute, second)); }
public static void main(String[] args) { AlarmClock alarmClock = new AlarmClock(7, 0, 0); alarmClock.start(); } }
Wake up! It′s 24 Aug 2003 07:00:00.023 Wake up! It′s 25 Aug 2003 07:00:00.001 Wake up! It′s 26 Aug 2003 07:00:00.058 Wake up! It′s 27 Aug 2003 07:00:00.015 Wake up! It′s 28 Aug 2003 07:00:00.002 ...
/** * A DailyIterator class returns a sequence of dates on subsequent days * representing the same time each day. */ public class DailyIterator implements ScheduleIterator { private final int hourOfDay, minute, second; private final Calendar calendar = Calendar.getInstance();
public DailyIterator(int hourOfDay, int minute, int second) { this(hourOfDay, minute, second, new Date()); }
public DailyIterator(int hourOfDay, int minute, int second, Date date) { this.hourOfDay = hourOfDay; this.minute = minute; this.second = second; calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, second); calendar.set(Calendar.MILLISECOND, 0); if (!calendar.getTime().before(date)) { calendar.add(Calendar.DATE, -1); } }
public Date next() { calendar.add(Calendar.DATE, 1); return calendar.getTime(); }
實時保證 在編寫使用計劃的應用程序時,一定要了解框架在時間方面有什么保證。我的任務是提前還是延遲執行?假如有提前或者延遲,偏差最大值是多少?不幸的是,對這些問題沒有簡單的答案。不過在實際中,它的行為對于很多應用程序已經足夠了。下面的討論假設系統時鐘是正確的(有關網絡時間協議(Network Time Protocol)的信息,請參閱 參考資料)。
Brian Goetz 的另一篇文章“Threading lightly, Part 2: RedUCing contention”(developerWorks,2001 年 9 月)分析了線程競用以及如何減少它。
這里是 Java SDK 1.4 中 Timer 和 TimerTask 的文檔。
Jcrontab 是用 Java 語言編寫的另一個調度程序,它的目標是取代 cron。
Network Time Protocol (NTP) 是一個同步計算機時鐘的協議。
Real-time Specification for Java 是 Java 平臺的一個擴展,它提供了實時保證。這個站點包括一個參考實現。
Joshua Bloch 的 Effective Java Programming Language Guiden(Addison-Wesley,2001 年)給出了很好的建議(例如,Item 51: Don′t depend on the thread scheduler)。
要了解有關世界各地日歷和使用它們的算法,請參閱 Nachum Dershowitz 和 Edward M. Reingold 的“Calendrical Calculations”(Cambridge University Press,1997 年)。
James Gosling、Bill Joy、Guy L. Steele Jr. 和 Gilad Bracha 編著的“The Java Language Specification, Second Edition ”(Addison-Wesley,2000 年)一書,第 17 章“Threads and Locks”描述了線程調度程序的行為。
Doug Lea 編著的“Concurrent Programming in Java: Design Principles and Patterns”(Addison-Wesley,1999 年)是一本好書,它深入探討了 Java 平臺中并發的各個方面。