jdk/src/share/classes/java/awt/EventDispatchThread.java
changeset 21263 77142152d60f
parent 18178 ee71c923891d
child 25206 f1ed7d27ec7f
equal deleted inserted replaced
21262:88e6e648168e 21263:77142152d60f
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package java.awt;
    26 package java.awt;
    27 
    27 
    28 import java.awt.event.InputEvent;
       
    29 import java.awt.event.MouseEvent;
    28 import java.awt.event.MouseEvent;
    30 import java.awt.event.ActionEvent;
    29 import java.awt.event.ActionEvent;
    31 import java.awt.event.WindowEvent;
    30 import java.awt.event.WindowEvent;
    32 import java.lang.reflect.Method;
       
    33 import java.security.AccessController;
       
    34 import sun.security.action.GetPropertyAction;
       
    35 import sun.awt.AWTAutoShutdown;
       
    36 import sun.awt.SunToolkit;
       
    37 import sun.awt.AppContext;
       
    38 
    31 
    39 import java.util.ArrayList;
    32 import java.util.ArrayList;
    40 import java.util.List;
       
    41 import sun.util.logging.PlatformLogger;
    33 import sun.util.logging.PlatformLogger;
    42 
    34 
    43 import sun.awt.dnd.SunDragSourceContextPeer;
    35 import sun.awt.dnd.SunDragSourceContextPeer;
    44 import sun.awt.EventQueueDelegate;
    36 import sun.awt.EventQueueDelegate;
    45 
    37 
    65 class EventDispatchThread extends Thread {
    57 class EventDispatchThread extends Thread {
    66 
    58 
    67     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventDispatchThread");
    59     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventDispatchThread");
    68 
    60 
    69     private EventQueue theQueue;
    61     private EventQueue theQueue;
    70     private boolean doDispatch = true;
    62     private volatile boolean doDispatch = true;
    71     private volatile boolean shutdown = false;
       
    72 
    63 
    73     private static final int ANY_EVENT = -1;
    64     private static final int ANY_EVENT = -1;
    74 
    65 
    75     private ArrayList<EventFilter> eventFilters = new ArrayList<EventFilter>();
    66     private ArrayList<EventFilter> eventFilters = new ArrayList<EventFilter>();
    76 
    67 
    84      */
    75      */
    85     public void stopDispatching() {
    76     public void stopDispatching() {
    86         doDispatch = false;
    77         doDispatch = false;
    87     }
    78     }
    88 
    79 
    89     public void interrupt() {
       
    90         shutdown = true;
       
    91         super.interrupt();
       
    92     }
       
    93 
       
    94     public void run() {
    80     public void run() {
    95         while (true) {
    81         try {
    96             try {
    82             pumpEvents(new Conditional() {
    97                 pumpEvents(new Conditional() {
    83                 public boolean evaluate() {
    98                     public boolean evaluate() {
    84                     return true;
    99                         return true;
    85                 }
   100                     }
    86             });
   101                 });
    87         } finally {
   102             } finally {
    88             getEventQueue().detachDispatchThread(this);
   103                 if(getEventQueue().detachDispatchThread(this, shutdown)) {
       
   104                     break;
       
   105                 }
       
   106             }
       
   107         }
    89         }
   108     }
    90     }
   109 
    91 
   110     void pumpEvents(Conditional cond) {
    92     void pumpEvents(Conditional cond) {
   111         pumpEvents(ANY_EVENT, cond);
    93         pumpEvents(ANY_EVENT, cond);
   128     }
   110     }
   129 
   111 
   130     void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) {
   112     void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) {
   131         addEventFilter(filter);
   113         addEventFilter(filter);
   132         doDispatch = true;
   114         doDispatch = true;
   133         shutdown |= isInterrupted();
   115         while (doDispatch && !isInterrupted() && cond.evaluate()) {
   134         while (doDispatch && !shutdown && cond.evaluate()) {
       
   135             pumpOneEventForFilters(id);
   116             pumpOneEventForFilters(id);
   136         }
   117         }
   137         removeEventFilter(filter);
   118         removeEventFilter(filter);
   138     }
   119     }
   139 
   120 
   221             if (delegate != null) {
   202             if (delegate != null) {
   222                 delegate.afterDispatch(event, handle);
   203                 delegate.afterDispatch(event, handle);
   223             }
   204             }
   224         }
   205         }
   225         catch (ThreadDeath death) {
   206         catch (ThreadDeath death) {
   226             shutdown = true;
   207             doDispatch = false;
   227             throw death;
   208             throw death;
   228         }
   209         }
   229         catch (InterruptedException interruptedException) {
   210         catch (InterruptedException interruptedException) {
   230             shutdown = true; // AppContext.dispose() interrupts all
   211             doDispatch = false; // AppContext.dispose() interrupts all
   231                              // Threads in the AppContext
   212                                 // Threads in the AppContext
   232         }
   213         }
   233         catch (Throwable e) {
   214         catch (Throwable e) {
   234             processException(e);
   215             processException(e);
   235         }
   216         }
   236     }
   217     }