1. /*
  2. * @(#)TimerQueue.java 1.30 00/04/04
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.util.*;
  12. /**
  13. * Internal class to manage all Timers using one thread.
  14. * TimerQueue manages a queue of Timers. The Timers are chained
  15. * together in a linked list sorted by the order in which they will expire.
  16. *
  17. * @version 1.30 04/04/00
  18. * @author Dave Moore
  19. */
  20. class TimerQueue implements Runnable
  21. {
  22. private static final Object sharedInstanceKey =
  23. new StringBuffer("TimerQueue.sharedInstanceKey");
  24. private static final Object expiredTimersKey =
  25. new StringBuffer("TimerQueue.expiredTimersKey");
  26. Timer firstTimer;
  27. boolean running;
  28. /* Lock object used in place of class object for synchronization.
  29. * (4187686)
  30. */
  31. private static final Object classLock = new Object();
  32. /**
  33. * Constructor for TimerQueue.
  34. */
  35. public TimerQueue() {
  36. super();
  37. // Now start the TimerQueue thread.
  38. start();
  39. }
  40. public static TimerQueue sharedInstance() {
  41. synchronized (classLock) {
  42. TimerQueue sharedInst = (TimerQueue)
  43. SwingUtilities.appContextGet(
  44. sharedInstanceKey);
  45. if (sharedInst == null) {
  46. sharedInst = new TimerQueue();
  47. SwingUtilities.appContextPut(sharedInstanceKey, sharedInst);
  48. }
  49. return sharedInst;
  50. }
  51. }
  52. synchronized void start() {
  53. if (running) {
  54. throw new RuntimeException("Can't start a TimerQueue " +
  55. "that is already running");
  56. }
  57. else {
  58. Thread timerThread = new Thread(this, "TimerQueue");
  59. try {
  60. timerThread.setDaemon(true);
  61. }
  62. catch (SecurityException e) {
  63. }
  64. timerThread.setPriority(Thread.NORM_PRIORITY);
  65. timerThread.start();
  66. running = true;
  67. }
  68. }
  69. synchronized void stop() {
  70. running = false;
  71. notify();
  72. }
  73. synchronized void addTimer(Timer timer, long expirationTime) {
  74. Timer previousTimer;
  75. Timer nextTimer;
  76. // If the Timer is already in the queue, then ignore the add.
  77. if (timer.running) {
  78. return;
  79. }
  80. previousTimer = null;
  81. nextTimer = firstTimer;
  82. // Insert the Timer into the linked list in the order they will
  83. // expire. If two timers expire at the same time, put the newer entry
  84. // later so they expire in the order they came in.
  85. while (nextTimer != null) {
  86. if (nextTimer.expirationTime > expirationTime) break;
  87. previousTimer = nextTimer;
  88. nextTimer = nextTimer.nextTimer;
  89. }
  90. if (previousTimer == null) {
  91. firstTimer = timer;
  92. }
  93. else {
  94. previousTimer.nextTimer = timer;
  95. }
  96. timer.expirationTime = expirationTime;
  97. timer.nextTimer = nextTimer;
  98. timer.running = true;
  99. notify();
  100. }
  101. synchronized void removeTimer(Timer timer) {
  102. Timer previousTimer;
  103. Timer nextTimer;
  104. boolean found;
  105. if (!timer.running) return;
  106. previousTimer = null;
  107. nextTimer = firstTimer;
  108. found = false;
  109. while (nextTimer != null) {
  110. if (nextTimer == timer) {
  111. found = true;
  112. break;
  113. }
  114. previousTimer = nextTimer;
  115. nextTimer = nextTimer.nextTimer;
  116. }
  117. if (!found) return;
  118. if (previousTimer == null) {
  119. firstTimer = timer.nextTimer;
  120. }
  121. else {
  122. previousTimer.nextTimer = timer.nextTimer;
  123. }
  124. timer.expirationTime = 0;
  125. timer.nextTimer = null;
  126. timer.running = false;
  127. }
  128. synchronized boolean containsTimer(Timer timer) {
  129. return timer.running;
  130. }
  131. /**
  132. * If there are a ton of timers, this method may never return. It loops
  133. * checking to see if the head of the Timer list has expired. If it has,
  134. * it posts the Timer and reschedules it if necessary.
  135. */
  136. synchronized long postExpiredTimers() {
  137. Timer timer;
  138. long currentTime;
  139. long timeToWait;
  140. // The timeToWait we return should never be negative and only be zero
  141. // when we have no Timers to wait for.
  142. do {
  143. timer = firstTimer;
  144. if (timer == null) return 0;
  145. currentTime = System.currentTimeMillis();
  146. timeToWait = timer.expirationTime - currentTime;
  147. if (timeToWait <= 0) {
  148. try {
  149. timer.post(); // have timer post an event
  150. }
  151. catch (SecurityException e) {
  152. }
  153. // Remove the timer from the queue
  154. removeTimer(timer);
  155. // This tries to keep the interval uniform at
  156. // the cost of drift.
  157. if (timer.isRepeats()) {
  158. addTimer(timer, currentTime + timer.getDelay());
  159. }
  160. }
  161. // Allow other threads to call addTimer() and removeTimer()
  162. // even when we are posting Timers like mad. Since the wait()
  163. // releases the lock, be sure not to maintain any state
  164. // between iterations of the loop.
  165. try {
  166. wait(1);
  167. }
  168. catch (InterruptedException e) {
  169. }
  170. } while (timeToWait <= 0);
  171. return timeToWait;
  172. }
  173. public synchronized void run() {
  174. long timeToWait;
  175. try {
  176. while (running) {
  177. timeToWait = postExpiredTimers();
  178. try {
  179. wait(timeToWait);
  180. }
  181. catch (InterruptedException e) {
  182. }
  183. }
  184. }
  185. catch (ThreadDeath td) {
  186. running = false;
  187. // Mark all the timers we contain as not being queued.
  188. Timer timer = firstTimer;
  189. while (timer != null) {
  190. timer.eventQueued = false;
  191. timer = timer.nextTimer;
  192. }
  193. SystemEventQueueUtilities.restartTimerQueueThread();
  194. throw td;
  195. }
  196. }
  197. public synchronized String toString() {
  198. StringBuffer buf;
  199. Timer nextTimer;
  200. buf = new StringBuffer();
  201. buf.append("TimerQueue (");
  202. nextTimer = firstTimer;
  203. while (nextTimer != null) {
  204. buf.append(nextTimer.toString());
  205. nextTimer = nextTimer.nextTimer;
  206. if (nextTimer != null) buf.append(", ");
  207. }
  208. buf.append(")");
  209. return buf.toString();
  210. }
  211. }