hmi.util
Class EventDispatchThreadHangMonitor

java.lang.Object
  extended by java.awt.EventQueue
      extended by hmi.util.EventDispatchThreadHangMonitor

public final class EventDispatchThreadHangMonitor
extends EventQueue

Monitors the AWT event dispatch thread for events that take longer than a certain time to be dispatched. The principle is to record the time at which we start processing an event, and have another thread check frequently to see if we're still processing. If the other thread notices that we've been processing a single event for too long, it prints a stack trace showing what the event dispatch thread is doing, and continues to time it until it finally finishes. This is useful in determining what code is causing your Java application's GUI to be unresponsive.

Author:
Elliott Hughes

Nested Class Summary
private  class EventDispatchThreadHangMonitor.HangChecker
          TimerTask that executes checks for hanging
 
Field Summary
private static long CHECK_INTERVAL_MS
           
private  Thread eventDispatchThread
           
private static EventQueue INSTANCE
           
private static long NO_CURRENT_EVENT
           
private  boolean reportedHang
           
private  long startedLastEventDispatchAt
           
private static long UNREASONABLE_DISPATCH_DURATION_MS
           
 
Constructor Summary
private EventDispatchThreadHangMonitor()
           
 
Method Summary
protected  void dispatchEvent(AWTEvent event)
          Overrides EventQueue.dispatchEvent to call our pre and post hooks either side of the system's event dispatch code.
 Thread getEventDispatchThread()
           
 boolean getReportedHang()
           
 long getStartedLastEventDispatchAt()
           
static void initMonitoring()
          Sets up hang detection for the event dispatch thread.
private  void initTimer()
          Sets up a timer to check for hangs frequently.
private  void postDispatchEvent()
          Reports the end of any ongoing hang, and notes that we're no longer processing an event.
private  void preDispatchEvent()
          Stores the time at which we started processing the current event.
 void setReportedHang(boolean val)
           
private  long timeSoFar()
          Returns how long we've been processing the current event (in milliseconds).
 
Methods inherited from class java.awt.EventQueue
getCurrentEvent, getMostRecentEventTime, getNextEvent, invokeAndWait, invokeLater, isDispatchThread, peekEvent, peekEvent, pop, postEvent, push
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INSTANCE

private static final EventQueue INSTANCE

CHECK_INTERVAL_MS

private static final long CHECK_INTERVAL_MS
See Also:
Constant Field Values

UNREASONABLE_DISPATCH_DURATION_MS

private static final long UNREASONABLE_DISPATCH_DURATION_MS
See Also:
Constant Field Values

NO_CURRENT_EVENT

private static final long NO_CURRENT_EVENT
See Also:
Constant Field Values

startedLastEventDispatchAt

private long startedLastEventDispatchAt

reportedHang

private boolean reportedHang

eventDispatchThread

private Thread eventDispatchThread
Constructor Detail

EventDispatchThreadHangMonitor

private EventDispatchThreadHangMonitor()
Method Detail

getStartedLastEventDispatchAt

public long getStartedLastEventDispatchAt()

getEventDispatchThread

public Thread getEventDispatchThread()

getReportedHang

public boolean getReportedHang()

setReportedHang

public void setReportedHang(boolean val)

initTimer

private void initTimer()
Sets up a timer to check for hangs frequently.


timeSoFar

private long timeSoFar()
Returns how long we've been processing the current event (in milliseconds).


initMonitoring

public static void initMonitoring()
Sets up hang detection for the event dispatch thread.


dispatchEvent

protected void dispatchEvent(AWTEvent event)
Overrides EventQueue.dispatchEvent to call our pre and post hooks either side of the system's event dispatch code.

Overrides:
dispatchEvent in class EventQueue

preDispatchEvent

private void preDispatchEvent()
Stores the time at which we started processing the current event.


postDispatchEvent

private void postDispatchEvent()
Reports the end of any ongoing hang, and notes that we're no longer processing an event.