jdk/src/share/classes/java/awt/EventQueue.java

Print this page
rev 5693 : 7192977: Issue in toolkit thread
Reviewed-by: skoivu, rupashka, art


 172      * Non-zero if a thread is waiting in getNextEvent(int) for an event of
 173      * a particular ID to be posted to the queue.
 174      */
 175     private volatile int waitForID;
 176 
 177     private final String name = "AWT-EventQueue-" + threadInitNumber.getAndIncrement();
 178 
 179     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventQueue");
 180 
 181     static {
 182         AWTAccessor.setEventQueueAccessor(
 183             new AWTAccessor.EventQueueAccessor() {
 184                 public Thread getDispatchThread(EventQueue eventQueue) {
 185                     return eventQueue.getDispatchThread();
 186                 }
 187                 public boolean isDispatchThreadImpl(EventQueue eventQueue) {
 188                     return eventQueue.isDispatchThreadImpl();
 189                 }
 190                 public void removeSourceEvents(EventQueue eventQueue,
 191                                                Object source,
 192                                                boolean removeAllEvents) {

 193                     eventQueue.removeSourceEvents(source, removeAllEvents);
 194                 }
 195                 public boolean noEvents(EventQueue eventQueue) {
 196                     return eventQueue.noEvents();
 197                 }
 198                 public void wakeup(EventQueue eventQueue, boolean isShutdown) {
 199                     eventQueue.wakeup(isShutdown);
 200                 }





 201             });
 202     }
 203 
 204     public EventQueue() {
 205         for (int i = 0; i < NUM_PRIORITIES; i++) {
 206             queues[i] = new Queue();
 207         }
 208         /*
 209          * NOTE: if you ever have to start the associated event dispatch
 210          * thread at this point, be aware of the following problem:
 211          * If this EventQueue instance is created in
 212          * SunToolkit.createNewAppContext() the started dispatch thread
 213          * may call AppContext.getAppContext() before createNewAppContext()
 214          * completes thus causing mess in thread group to appcontext mapping.
 215          */
 216 
 217         pushPopLock = (Lock)AppContext.getAppContext().get(AppContext.EVENT_QUEUE_LOCK_KEY);
 218         pushPopCond = (Condition)AppContext.getAppContext().get(AppContext.EVENT_QUEUE_COND_KEY);
 219     }
 220 


1216      * This will happen after all pending events are processed.
1217      * The call blocks until this has happened.  This method
1218      * will throw an Error if called from the
1219      * {@link #isDispatchThread event dispatcher thread}.
1220      *
1221      * @param runnable  the <code>Runnable</code> whose <code>run</code>
1222      *                  method should be executed
1223      *                  synchronously in the
1224      *                  {@link #isDispatchThread event dispatch thread}
1225      *                  of {@link Toolkit#getSystemEventQueue the system EventQueue}
1226      * @exception       InterruptedException  if any thread has
1227      *                  interrupted this thread
1228      * @exception       InvocationTargetException  if an throwable is thrown
1229      *                  when running <code>runnable</code>
1230      * @see             #invokeLater
1231      * @see             Toolkit#getSystemEventQueue
1232      * @see             #isDispatchThread
1233      * @since           1.2
1234      */
1235     public static void invokeAndWait(Runnable runnable)
1236              throws InterruptedException, InvocationTargetException {



1237 



1238         if (EventQueue.isDispatchThread()) {
1239             throw new Error("Cannot call invokeAndWait from the event dispatcher thread");
1240         }
1241 
1242         class AWTInvocationLock {}
1243         Object lock = new AWTInvocationLock();
1244 
1245         InvocationEvent event =
1246             new InvocationEvent(Toolkit.getDefaultToolkit(), runnable, lock,
1247                                 true);
1248 
1249         synchronized (lock) {
1250             Toolkit.getEventQueue().postEvent(event);
1251             while (!event.isDispatched()) {
1252                 lock.wait();
1253             }
1254         }
1255 
1256         Throwable eventThrowable = event.getThrowable();
1257         if (eventThrowable != null) {
1258             throw new InvocationTargetException(eventThrowable);
1259         }
1260     }
1261 
1262     /*
1263      * Called from PostEventQueue.postEvent to notify that a new event
1264      * appeared. First it proceeds to the EventQueue on the top of the
1265      * stack, then notifies the associated dispatch thread if it exists
1266      * or starts a new one otherwise.
1267      */




 172      * Non-zero if a thread is waiting in getNextEvent(int) for an event of
 173      * a particular ID to be posted to the queue.
 174      */
 175     private volatile int waitForID;
 176 
 177     private final String name = "AWT-EventQueue-" + threadInitNumber.getAndIncrement();
 178 
 179     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventQueue");
 180 
 181     static {
 182         AWTAccessor.setEventQueueAccessor(
 183             new AWTAccessor.EventQueueAccessor() {
 184                 public Thread getDispatchThread(EventQueue eventQueue) {
 185                     return eventQueue.getDispatchThread();
 186                 }
 187                 public boolean isDispatchThreadImpl(EventQueue eventQueue) {
 188                     return eventQueue.isDispatchThreadImpl();
 189                 }
 190                 public void removeSourceEvents(EventQueue eventQueue,
 191                                                Object source,
 192                                                boolean removeAllEvents)
 193                 {
 194                     eventQueue.removeSourceEvents(source, removeAllEvents);
 195                 }
 196                 public boolean noEvents(EventQueue eventQueue) {
 197                     return eventQueue.noEvents();
 198                 }
 199                 public void wakeup(EventQueue eventQueue, boolean isShutdown) {
 200                     eventQueue.wakeup(isShutdown);
 201                 }
 202                 public void invokeAndWait(Object source, Runnable r)
 203                     throws InterruptedException, InvocationTargetException
 204                 {
 205                     EventQueue.invokeAndWait(source, r);
 206                 }
 207             });
 208     }
 209 
 210     public EventQueue() {
 211         for (int i = 0; i < NUM_PRIORITIES; i++) {
 212             queues[i] = new Queue();
 213         }
 214         /*
 215          * NOTE: if you ever have to start the associated event dispatch
 216          * thread at this point, be aware of the following problem:
 217          * If this EventQueue instance is created in
 218          * SunToolkit.createNewAppContext() the started dispatch thread
 219          * may call AppContext.getAppContext() before createNewAppContext()
 220          * completes thus causing mess in thread group to appcontext mapping.
 221          */
 222 
 223         pushPopLock = (Lock)AppContext.getAppContext().get(AppContext.EVENT_QUEUE_LOCK_KEY);
 224         pushPopCond = (Condition)AppContext.getAppContext().get(AppContext.EVENT_QUEUE_COND_KEY);
 225     }
 226 


1222      * This will happen after all pending events are processed.
1223      * The call blocks until this has happened.  This method
1224      * will throw an Error if called from the
1225      * {@link #isDispatchThread event dispatcher thread}.
1226      *
1227      * @param runnable  the <code>Runnable</code> whose <code>run</code>
1228      *                  method should be executed
1229      *                  synchronously in the
1230      *                  {@link #isDispatchThread event dispatch thread}
1231      *                  of {@link Toolkit#getSystemEventQueue the system EventQueue}
1232      * @exception       InterruptedException  if any thread has
1233      *                  interrupted this thread
1234      * @exception       InvocationTargetException  if an throwable is thrown
1235      *                  when running <code>runnable</code>
1236      * @see             #invokeLater
1237      * @see             Toolkit#getSystemEventQueue
1238      * @see             #isDispatchThread
1239      * @since           1.2
1240      */
1241     public static void invokeAndWait(Runnable runnable)
1242         throws InterruptedException, InvocationTargetException
1243     {
1244         invokeAndWait(Toolkit.getDefaultToolkit(), runnable);
1245     }
1246 
1247     static void invokeAndWait(Object source, Runnable runnable)
1248         throws InterruptedException, InvocationTargetException
1249     {
1250         if (EventQueue.isDispatchThread()) {
1251             throw new Error("Cannot call invokeAndWait from the event dispatcher thread");
1252         }
1253 
1254         class AWTInvocationLock {}
1255         Object lock = new AWTInvocationLock();
1256 
1257         InvocationEvent event =
1258             new InvocationEvent(source, runnable, lock, true);

1259 
1260         synchronized (lock) {
1261             Toolkit.getEventQueue().postEvent(event);
1262             while (!event.isDispatched()) {
1263                 lock.wait();
1264             }
1265         }
1266 
1267         Throwable eventThrowable = event.getThrowable();
1268         if (eventThrowable != null) {
1269             throw new InvocationTargetException(eventThrowable);
1270         }
1271     }
1272 
1273     /*
1274      * Called from PostEventQueue.postEvent to notify that a new event
1275      * appeared. First it proceeds to the EventQueue on the top of the
1276      * stack, then notifies the associated dispatch thread if it exists
1277      * or starts a new one otherwise.
1278      */