1. /*
  2. * @(#)TimerQueue.java 1.37 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.util.*;
  9. /**
  10. * Internal class to manage all Timers using one thread.
  11. * TimerQueue manages a queue of Timers. The Timers are chained
  12. * together in a linked list sorted by the order in which they will expire.
  13. *
  14. * @version 1.37 12/19/03
  15. * @author Dave Moore
  16. */
  17. class TimerQueue implements Runnable
  18. {
  19. private static final Object sharedInstanceKey =
  20. new StringBuffer("TimerQueue.sharedInstanceKey");
  21. private static final Object expiredTimersKey =
  22. new StringBuffer("TimerQueue.expiredTimersKey");
  23. Timer firstTimer;
  24. boolean running;
  25. /* Lock object used in place of class object for synchronization.
  26. * (4187686)
  27. */
  28. private static final Object classLock = new Object();
  29. /**
  30. * Constructor for TimerQueue.
  31. */
  32. public TimerQueue() {
  33. super();
  34. // Now start the TimerQueue thread.
  35. start();
  36. }
  37. public static TimerQueue sharedInstance() {
  38. synchronized (classLock) {
  39. TimerQueue sharedInst = (TimerQueue)
  40. SwingUtilities.appContextGet(
  41. sharedInstanceKey);
  42. if (sharedInst == null) {
  43. sharedInst = new TimerQueue();
  44. SwingUtilities.appContextPut(sharedInstanceKey, sharedInst);
  45. }
  46. return sharedInst;
  47. }
  48. }
  49. synchronized void start() {
  50. if (running) {
  51. throw new RuntimeException("Can't start a TimerQueue " +
  52. "that is already running");
  53. }
  54. else {
  55. java.security.AccessController.doPrivileged(
  56. new java.security.PrivilegedAction() {
  57. public Object run() {
  58. Thread timerThread = new Thread(TimerQueue.this,
  59. "TimerQueue");
  60. timerThread.setDaemon(true);
  61. timerThread.setPriority(Thread.NORM_PRIORITY);
  62. timerThread.start();
  63. return null;
  64. }
  65. });
  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. // Allow other threads to call addTimer() and removeTimer()
  161. // even when we are posting Timers like mad. Since the wait()
  162. // releases the lock, be sure not to maintain any state
  163. // between iterations of the loop.
  164. try {
  165. wait(1);
  166. }
  167. catch (InterruptedException e) {
  168. }
  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.cancelEvent();
  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. }