APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.awt

public Class EventQueue

Additional top-level class in compilation unit: Queue.

extends Object
Class Inheritance
Imports
java.awt.event.*, java.awt.peer.ComponentPeer, java.lang.ref.WeakReference, java.lang.reflect.InvocationTargetException, java.util.EmptyStackException, sun.awt.*, sun.awt.dnd.SunDropTargetEvent, sun.util.logging.PlatformLogger, java.util.concurrent.locks.Condition, .Lock, .ReentrantLock, java.util.concurrent.atomic.AtomicInteger

EventQueue is a platform-independent class that queues events, both from the underlying peer classes and from trusted application classes.

It encapsulates asynchronous event dispatch machinery which extracts events from the queue and dispatches them by calling dispatchEvent(AWTEvent) method on this EventQueue with the event to be dispatched as an argument. The particular behavior of this machinery is implementation-dependent. The only requirements are that events which were actually enqueued to this queue (note that events being posted to the EventQueue can be coalesced) are dispatched:

Sequentially.
That is, it is not permitted that several events from this queue are dispatched simultaneously.
In the same order as they are enqueued.
That is, if AWTEvent A is enqueued to the EventQueue before AWTEvent B then event B will not be dispatched before event A.

For information on the threading issues of the event dispatch machinery, see AWT Threading Issues.

Authors
Thomas Ball, Fred Ecks, David Mendenhall
Since
1.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private static final int
private final ClassLoader
private WeakReference<AWTEvent>
currentEvent

The modifiers field of the current event, if the current event is an InputEvent or ActionEvent.

private EventDispatchThread
private static final int
private static final Runnable
private static volatile PlatformLogger
private FwDispatcher
private static boolean
private static final int
private static final int
private long
private long
private static final int
private final String
private EventQueue
private static final int
private static final int
private static final int
private static final int
private EventQueue
private static final Condition
private static final Lock
private Queue[]
private final ThreadGroup
private static final AtomicInteger
private static final int
private static final int
private volatile int

Constructor Summary

AccessConstructor and Description
public
EventQueue()

Initializes a new instance of EventQueue.

Method Summary

Modifier and TypeMethod and Description
private void
private boolean
coalesceEvent(AWTEvent e, int priority)

private boolean
private boolean
coalesceOtherEvent(AWTEvent e, int priority)

private boolean
private boolean
public SecondaryLoop

Returns:

secondaryLoop A new secondary loop object, which can be used to launch a new nested event loop and dispatch events from this queue
createSecondaryLoop
()

Creates a new secondary loop associated with this event queue.

pack-priv SecondaryLoop
createSecondaryLoop(Conditional cond, EventFilter filter, long interval)

pack-priv final void
protected void
dispatchEvent(final AWTEvent
an instance of java.awt.AWTEvent, or a subclass of it
event
)

Dispatches an event.

private void
dispatchEventImpl(final AWTEvent event, final Object src)

Called from dispatchEvent()

private static int
public static AWTEvent

Returns:

the event currently being dispatched, or null if this method is invoked on a thread other than an event dispatching thread
getCurrentEvent
()

Returns the event currently being dispatched by the EventQueue associated with the calling thread.

private AWTEvent
pack-priv final EventDispatchThread
private static final PlatformLogger
public static long

Returns:

the timestamp of the last InputEvent, ActionEvent, or InvocationEvent to be dispatched, or System.currentTimeMillis() if this method is invoked on a thread other than an event dispatching thread
getMostRecentEventTime
()

Returns the timestamp of the most recent event that had a timestamp, and that was dispatched from the EventQueue associated with the calling thread.

pack-priv long
getMostRecentEventTimeEx()

Returns: most recent event time on all threads.

private long
pack-priv synchronized long
public AWTEvent

Returns:

the next AWTEvent
getNextEvent
()

Removes an event from the EventQueue and returns it.

pack-priv AWTEvent
getNextEvent(int id)

pack-priv AWTEvent
private static int
pack-priv final void
public static void
invokeAndWait(Runnable
the Runnable whose run method should be executed synchronously in the event dispatch thread of the system EventQueue
runnable
)

Causes runnable to have its run method called in the dispatch thread of the system EventQueue.

pack-priv static void
invokeAndWait(Object source, Runnable runnable)

public static void
invokeLater(Runnable
the Runnable whose run method should be executed asynchronously in the event dispatch thread of the system EventQueue
runnable
)

Causes runnable to have its run method called in the dispatch thread of the system EventQueue.

public static boolean

Returns:

true if running in the current AWT EventQueue's dispatch thread
isDispatchThread
()

Returns true if the calling thread is the current AWT EventQueue's dispatch thread.

pack-priv final boolean
private PaintEvent
private boolean

Returns:

whether an event is pending on any of the separate Queues
noEvents
()

Returns whether an event is pending on any of the separate Queues.

public AWTEvent

Returns:

the first event
peekEvent
()

Returns the first event on the EventQueue without removing it.

public AWTEvent

Returns:

the first event of the specified id or null if there is no such event
peekEvent
(int
the id of the type of event desired
id
)

Returns the first event with the specified id, if any.

protected void
pop()

Stops dispatching events using this EventQueue.

public void
postEvent(AWTEvent
an instance of java.awt.AWTEvent, or a subclass of it
theEvent
)

Posts a 1.1-style event to the EventQueue.

private void
postEvent(AWTEvent
an instance of java.awt.AWTEvent, or a subclass of it
theEvent
,
int
the desired priority of the event
priority
)

Posts the event to the internal Queue of specified priority, coalescing as appropriate.

private void
postEventPrivate(AWTEvent
an instance of java.awt.AWTEvent, or a subclass of it
theEvent
)

Posts a 1.1-style event to the EventQueue.

public void
push(EventQueue
an EventQueue (or subclass thereof) instance to be use
newEventQueue
)

Replaces the existing EventQueue with the specified one.

pack-priv final void
removeSourceEvents(Object source, boolean removeAllEvents)

pack-priv static void
private void
private void
private void
private void
wakeup(boolean isShutdown)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CACHE_LENGTHback to summary
private static final int CACHE_LENGTH
classLoaderback to summary
private final ClassLoader classLoader
currentEventback to summary
private WeakReference<AWTEvent> currentEvent

The modifiers field of the current event, if the current event is an InputEvent or ActionEvent.

dispatchThreadback to summary
private EventDispatchThread dispatchThread
DRAGback to summary
private static final int DRAG
dummyRunnableback to summary
private static final Runnable dummyRunnable
eventLogback to summary
private static volatile PlatformLogger eventLog
fwDispatcherback to summary
private FwDispatcher fwDispatcher
fxAppThreadIsDispatchThreadback to summary
private static boolean fxAppThreadIsDispatchThread
HIGH_PRIORITYback to summary
private static final int HIGH_PRIORITY
LOW_PRIORITYback to summary
private static final int LOW_PRIORITY
mostRecentEventTimeback to summary
private long mostRecentEventTime
mostRecentKeyEventTimeback to summary
private long mostRecentKeyEventTime
MOVEback to summary
private static final int MOVE
nameback to summary
private final String name
nextQueueback to summary
private EventQueue nextQueue
NORM_PRIORITYback to summary
private static final int NORM_PRIORITY
NUM_PRIORITIESback to summary
private static final int NUM_PRIORITIES
PAINTback to summary
private static final int PAINT
PEERback to summary
private static final int PEER
previousQueueback to summary
private EventQueue previousQueue
pushPopCondback to summary
private static final Condition pushPopCond
pushPopLockback to summary
private static final Lock pushPopLock
queuesback to summary
private Queue[] queues
threadGroupback to summary
private final ThreadGroup threadGroup
threadInitNumberback to summary
private static final AtomicInteger threadInitNumber
ULTIMATE_PRIORITYback to summary
private static final int ULTIMATE_PRIORITY
UPDATEback to summary
private static final int UPDATE
waitForIDback to summary
private volatile int waitForID

Constructor Detail

EventQueueback to summary
public EventQueue()

Initializes a new instance of EventQueue.

Method Detail

cacheEQItemback to summary
private void cacheEQItem(EventQueueItem entry)
coalesceEventback to summary
private boolean coalesceEvent(AWTEvent e, int priority)
coalesceMouseEventback to summary
private boolean coalesceMouseEvent(MouseEvent e)
coalesceOtherEventback to summary
private boolean coalesceOtherEvent(AWTEvent e, int priority)
coalescePaintEventback to summary
private boolean coalescePaintEvent(PaintEvent e)
coalescePeerEventback to summary
private boolean coalescePeerEvent(PeerEvent e)
createSecondaryLoopback to summary
public SecondaryLoop createSecondaryLoop()

Creates a new secondary loop associated with this event queue. Use the SecondaryLoop#enter and SecondaryLoop#exit methods to start and stop the event loop and dispatch the events from this queue.

Returns:SecondaryLoop

secondaryLoop A new secondary loop object, which can be used to launch a new nested event loop and dispatch events from this queue

Since
1.7
See Also
SecondaryLoop#enter, SecondaryLoop#exit
createSecondaryLoopback to summary
pack-priv SecondaryLoop createSecondaryLoop(Conditional cond, EventFilter filter, long interval)
detachDispatchThreadback to summary
pack-priv final void detachDispatchThread(EventDispatchThread edt)
dispatchEventback to summary
protected void dispatchEvent(final AWTEvent event)

Dispatches an event. The manner in which the event is dispatched depends upon the type of the event and the type of the event's source object:

Event types, source types, and dispatch methods
Event Type Source Type Dispatched To
ActiveEvent Any event.dispatch()
Other Component source.dispatchEvent(AWTEvent)
Other MenuComponent source.dispatchEvent(AWTEvent)
Other Other No action (ignored)
Parameters
event:AWTEvent

an instance of java.awt.AWTEvent, or a subclass of it

Exceptions
NullPointerException:
if event is null
Since
1.2
dispatchEventImplback to summary
private void dispatchEventImpl(final AWTEvent event, final Object src)

Called from dispatchEvent()

eventToCacheIndexback to summary
private static int eventToCacheIndex(AWTEvent e)
getCurrentEventback to summary
public static AWTEvent getCurrentEvent()

Returns the event currently being dispatched by the EventQueue associated with the calling thread. This is useful if a method needs access to the event, but was not designed to receive a reference to it as an argument. Note that this method should only be invoked from an application's event dispatching thread. If this method is invoked from another thread, null will be returned.

Returns:AWTEvent

the event currently being dispatched, or null if this method is invoked on a thread other than an event dispatching thread

Since
1.4
getCurrentEventImplback to summary
private AWTEvent getCurrentEventImpl()
getDispatchThreadback to summary
pack-priv final EventDispatchThread getDispatchThread()
getEventLogback to summary
private static final PlatformLogger getEventLog()
getMostRecentEventTimeback to summary
public static long getMostRecentEventTime()

Returns the timestamp of the most recent event that had a timestamp, and that was dispatched from the EventQueue associated with the calling thread. If an event with a timestamp is currently being dispatched, its timestamp will be returned. If no events have yet been dispatched, the EventQueue's initialization time will be returned instead.In the current version of the JDK, only InputEvents, ActionEvents, and InvocationEvents have timestamps; however, future versions of the JDK may add timestamps to additional event types. Note that this method should only be invoked from an application's event dispatching thread. If this method is invoked from another thread, the current system time (as reported by System.currentTimeMillis()) will be returned instead.

Returns:long

the timestamp of the last InputEvent, ActionEvent, or InvocationEvent to be dispatched, or System.currentTimeMillis() if this method is invoked on a thread other than an event dispatching thread

Since
1.4
See Also
java.awt.event.InputEvent#getWhen, java.awt.event.ActionEvent#getWhen, java.awt.event.InvocationEvent#getWhen, isDispatchThread
getMostRecentEventTimeExback to summary
pack-priv long getMostRecentEventTimeEx()
Returns:long

most recent event time on all threads.

getMostRecentEventTimeImplback to summary
private long getMostRecentEventTimeImpl()
getMostRecentKeyEventTimeback to summary
pack-priv synchronized long getMostRecentKeyEventTime()
getNextEventback to summary
public AWTEvent getNextEvent() throws InterruptedException

Removes an event from the EventQueue and returns it. This method will block until an event has been posted by another thread.

Returns:AWTEvent

the next AWTEvent

Exceptions
InterruptedException:
if any thread has interrupted this thread
getNextEventback to summary
pack-priv AWTEvent getNextEvent(int id) throws InterruptedException
getNextEventPrivateback to summary
pack-priv AWTEvent getNextEventPrivate() throws InterruptedException
getPriorityback to summary
private static int getPriority(AWTEvent theEvent)
initDispatchThreadback to summary
pack-priv final void initDispatchThread()
Annotations
@SuppressWarnings:removal
invokeAndWaitback to summary
public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException

Causes runnable to have its run method called in the dispatch thread of the system EventQueue. This will happen after all pending events are processed. The call blocks until this has happened. This method will throw an Error if called from the event dispatcher thread.

Parameters
runnable:Runnable

the Runnable whose run method should be executed synchronously in the event dispatch thread of the system EventQueue

Exceptions
InterruptedException:
if any thread has interrupted this thread
InvocationTargetException:
if an throwable is thrown when running runnable
Since
1.2
See Also
invokeLater, Toolkit#getSystemEventQueue, isDispatchThread
invokeAndWaitback to summary
pack-priv static void invokeAndWait(Object source, Runnable runnable) throws InterruptedException, InvocationTargetException
invokeLaterback to summary
public static void invokeLater(Runnable runnable)

Causes runnable to have its run method called in the dispatch thread of the system EventQueue. This will happen after all pending events are processed.

Parameters
runnable:Runnable

the Runnable whose run method should be executed asynchronously in the event dispatch thread of the system EventQueue

Since
1.2
See Also
invokeAndWait, Toolkit#getSystemEventQueue, isDispatchThread
isDispatchThreadback to summary
public static boolean isDispatchThread()

Returns true if the calling thread is the current AWT EventQueue's dispatch thread. Use this method to ensure that a particular task is being executed (or not being) there.

Note

use the invokeLater or invokeAndWait methods to execute a task in the current AWT EventQueue's dispatch thread.

Returns:boolean

true if running in the current AWT EventQueue's dispatch thread

Since
1.2
See Also
invokeLater, invokeAndWait, Toolkit#getSystemEventQueue
isDispatchThreadImplback to summary
pack-priv final boolean isDispatchThreadImpl()
mergePaintEventsback to summary
private PaintEvent mergePaintEvents(PaintEvent a, PaintEvent b)
noEventsback to summary
private boolean noEvents()

Returns whether an event is pending on any of the separate Queues.

Returns:boolean

whether an event is pending on any of the separate Queues

peekEventback to summary
public AWTEvent peekEvent()

Returns the first event on the EventQueue without removing it.

Returns:AWTEvent

the first event

peekEventback to summary
public AWTEvent peekEvent(int id)

Returns the first event with the specified id, if any.

Parameters
id:int

the id of the type of event desired

Returns:AWTEvent

the first event of the specified id or null if there is no such event

popback to summary
protected void pop() throws EmptyStackException

Stops dispatching events using this EventQueue. Any pending events are transferred to the previous EventQueue for processing.

Warning

To avoid deadlock, do not declare this method synchronized in a subclass.

Exceptions
EmptyStackException:
if no previous push was made on this EventQueue
Since
1.2
See Also
java.awt.EventQueue#push
postEventback to summary
public void postEvent(AWTEvent theEvent)

Posts a 1.1-style event to the EventQueue. If there is an existing event on the queue with the same ID and event source, the source Component's coalesceEvents method will be called.

Parameters
theEvent:AWTEvent

an instance of java.awt.AWTEvent, or a subclass of it

Exceptions
NullPointerException:
if theEvent is null
postEventback to summary
private void postEvent(AWTEvent theEvent, int priority)

Posts the event to the internal Queue of specified priority, coalescing as appropriate.

Parameters
theEvent:AWTEvent

an instance of java.awt.AWTEvent, or a subclass of it

priority:int

the desired priority of the event

postEventPrivateback to summary
private void postEventPrivate(AWTEvent theEvent)

Posts a 1.1-style event to the EventQueue. If there is an existing event on the queue with the same ID and event source, the source Component's coalesceEvents method will be called.

Parameters
theEvent:AWTEvent

an instance of java.awt.AWTEvent, or a subclass of it

pushback to summary
public void push(EventQueue newEventQueue)

Replaces the existing EventQueue with the specified one. Any pending events are transferred to the new EventQueue for processing by it.

Parameters
newEventQueue:EventQueue

an EventQueue (or subclass thereof) instance to be use

Exceptions
NullPointerException:
if newEventQueue is null
Since
1.2
See Also
java.awt.EventQueue#pop
removeSourceEventsback to summary
pack-priv final void removeSourceEvents(Object source, boolean removeAllEvents)
setCurrentEventAndMostRecentTimeback to summary
pack-priv static void setCurrentEventAndMostRecentTime(AWTEvent e)
setCurrentEventAndMostRecentTimeImplback to summary
private void setCurrentEventAndMostRecentTimeImpl(AWTEvent e)
setFwDispatcherback to summary
private void setFwDispatcher(FwDispatcher dispatcher)
uncacheEQItemback to summary
private void uncacheEQItem(EventQueueItem entry)
wakeupback to summary
private void wakeup(boolean isShutdown)
java.awt back to summary

private Class EventQueue.FwSecondaryLoopWrapper

extends Object
implements SecondaryLoop
Class Inheritance
All Implemented Interfaces
java.awt.SecondaryLoop

Field Summary

Modifier and TypeField and Description
private final EventFilter
private final SecondaryLoop

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
enter()

Implements java.awt.SecondaryLoop.enter.

Blocks the execution of the current thread and enters a new secondary event loop on the event dispatch thread.

public boolean
exit()

Implements java.awt.SecondaryLoop.exit.

Unblocks the execution of the thread blocked by the enter method and exits the secondary loop.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

filterback to summary
private final EventFilter filter
loopback to summary
private final SecondaryLoop loop

Constructor Detail

FwSecondaryLoopWrapperback to summary
public FwSecondaryLoopWrapper(SecondaryLoop loop, EventFilter filter)

Method Detail

enterback to summary
public boolean enter()

Implements java.awt.SecondaryLoop.enter.

Doc from java.awt.SecondaryLoop.enter.

Blocks the execution of the current thread and enters a new secondary event loop on the event dispatch thread.

This method can be called by any thread including the event dispatch thread. This thread will be blocked until the exit method is called or the loop is terminated. A new secondary loop will be created on the event dispatch thread for dispatching events in either case.

This method can only start one new event loop at a time per object. If a secondary event loop has already been started by this object and is currently still running, this method returns false to indicate that it was not successful in starting a new event loop. Otherwise, this method blocks the calling thread and later returns true when the new event loop is terminated. At such time, this object can again be used to start another new event loop.

Annotations
@Override
exitback to summary
public boolean exit()

Implements java.awt.SecondaryLoop.exit.

Doc from java.awt.SecondaryLoop.exit.

Unblocks the execution of the thread blocked by the enter method and exits the secondary loop.

This method resumes the thread that called the enter method and exits the secondary loop that was created when the enter method was invoked.

Note that if any other secondary loop is started while this loop is running, the blocked thread will not resume execution until the nested loop is terminated.

If this secondary loop has not been started with the enter method, or this secondary loop has already finished with the exit method, this method returns false, otherwise true is returned.

Annotations
@Override