APIdia Launch Partner
Top Description Fields Constructors Methods
sun.awt

public final Class AWTAutoShutdown

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Imports
java.awt.AWTEvent, .Toolkit, java.util.HashSet, .IdentityHashMap, .Map, .Set, sun.awt.util.ThreadGroupUtils, sun.util.logging.PlatformLogger

This class is to let AWT shutdown automatically when a user is done with AWT. It tracks AWT state using the following parameters:

AWT is considered to be in ready-to-shutdown state when peerMap is empty and toolkitThreadBusy is false and busyThreadSet is empty. The internal AWTAutoShutdown logic secures that the single non-daemon thread (blockerThread) is running when AWT is not in ready-to-shutdown state. This blocker thread is to prevent AWT from exiting since the toolkit thread is now daemon and all the event dispatch threads are started only when needed. Once it is detected that AWT is in ready-to-shutdown state this blocker thread waits for a certain timeout and if AWT state doesn't change during timeout this blocker thread terminates all the event dispatch threads and exits.

Field Summary

Modifier and TypeField and Description
private final Object
activationLock

This lock object is to secure that when a new blocker thread is started it will be the first who acquire the main lock after the thread that created the new blocker released the main lock by calling lock.wait() to wait for the blocker to start.

private Thread
blockerThread

References the alive non-daemon thread that is currently used for keeping AWT from exiting.

private final Set<Thread>
busyThreadSet

This set keeps references to all the event dispatch threads that are busy at this moment, i.e. those that are not waiting for a new event to appear in their event queue.

private final Object
mainLock

This lock object is used to synchronize shutdown operations.

private final Map<Object, Object>
peerMap

This is a map between components and their peers.

private static final int
SAFETY_TIMEOUT

Once we detect that AWT is ready to shutdown we wait for a certain timeout to pass before stopping event dispatch threads.

private static final AWTAutoShutdown
private boolean
timeoutPassed

We need this flag to secure that AWT state hasn't changed while we were waiting for the safety timeout to pass.

private boolean
toolkitThreadBusy

Indicates whether the toolkit thread is waiting for a new native event to appear or is dispatching an event.

Constructor Summary

AccessConstructor and Description
private
AWTAutoShutdown()

Constructor method is intentionally made private to secure a single instance.

Method Summary

Modifier and TypeMethod and Description
private void
activateBlockerThread()

Creates and starts a new blocker thread.

pack-priv void
public static AWTAutoShutdown
getInstance()

Returns reference to a single AWTAutoShutdown instance.

pack-priv Object
getPeer(final Object target)

pack-priv static AWTEvent
private boolean

Returns:

true if AWT is in ready-to-shutdown state.
isReadyToShutdown
()

Determine whether AWT is currently in ready-to-shutdown state.

pack-priv void
notifyPeerMapUpdated()

Notify that the peermap has been updated, that means a new peer has been created or some existing peer has been disposed.

public void
notifyThreadBusy(final Thread
thread to be added to this set, if not present.
thread
)

Add a specified thread to the set of busy event dispatch threads.

public void
notifyThreadFree(final Thread
thread to be removed from this set, if present.
thread
)

Remove a specified thread from the set of busy event dispatch threads.

public static void
notifyToolkitThreadBusy()

Notify that the toolkit thread is not waiting for a native event to appear in its queue.

public static void
notifyToolkitThreadFree()

Notify that the toolkit thread is waiting for a native event to appear in its queue.

pack-priv void
registerPeer(final Object target, final Object peer)

public void
run()

Implements java.lang.Runnable.run.

Implementation of the Runnable interface.

private void
setToolkitBusy(final boolean
true if the toolkit thread state changes from idle to busy.
busy
)

Notify about the toolkit thread state change.

pack-priv void
unregisterPeer(final Object target, final Object peer)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

activationLockback to summary
private final Object activationLock

This lock object is to secure that when a new blocker thread is started it will be the first who acquire the main lock after the thread that created the new blocker released the main lock by calling lock.wait() to wait for the blocker to start.

blockerThreadback to summary
private Thread blockerThread

References the alive non-daemon thread that is currently used for keeping AWT from exiting.

busyThreadSetback to summary
private final Set<Thread> busyThreadSet

This set keeps references to all the event dispatch threads that are busy at this moment, i.e. those that are not waiting for a new event to appear in their event queue. Access is synchronized on the main lock object.

mainLockback to summary
private final Object mainLock

This lock object is used to synchronize shutdown operations.

peerMapback to summary
private final Map<Object, Object> peerMap

This is a map between components and their peers. we should work with in under activationLock&mainLock lock.

SAFETY_TIMEOUTback to summary
private static final int SAFETY_TIMEOUT

Once we detect that AWT is ready to shutdown we wait for a certain timeout to pass before stopping event dispatch threads.

theInstanceback to summary
private static final AWTAutoShutdown theInstance
timeoutPassedback to summary
private boolean timeoutPassed

We need this flag to secure that AWT state hasn't changed while we were waiting for the safety timeout to pass.

toolkitThreadBusyback to summary
private boolean toolkitThreadBusy

Indicates whether the toolkit thread is waiting for a new native event to appear or is dispatching an event.

Constructor Detail

AWTAutoShutdownback to summary
private AWTAutoShutdown()

Constructor method is intentionally made private to secure a single instance. Use getInstance() to reference it.

See Also
AWTAutoShutdown#getInstance

Method Detail

activateBlockerThreadback to summary
private void activateBlockerThread()

Creates and starts a new blocker thread. Doesn't return until the new blocker thread starts.

dumpPeersback to summary
pack-priv void dumpPeers(final PlatformLogger aLog)
getInstanceback to summary
public static AWTAutoShutdown getInstance()

Returns reference to a single AWTAutoShutdown instance.

getPeerback to summary
pack-priv Object getPeer(final Object target)
getShutdownEventback to summary
pack-priv static AWTEvent getShutdownEvent()
isReadyToShutdownback to summary
private boolean isReadyToShutdown()

Determine whether AWT is currently in ready-to-shutdown state. AWT is considered to be in ready-to-shutdown state if peerMap is empty and toolkitThreadBusy is false and busyThreadSet is empty.

Returns:boolean

true if AWT is in ready-to-shutdown state.

notifyPeerMapUpdatedback to summary
pack-priv void notifyPeerMapUpdated()

Notify that the peermap has been updated, that means a new peer has been created or some existing peer has been disposed.

See Also
AWTAutoShutdown#isReadyToShutdown
notifyThreadBusyback to summary
public void notifyThreadBusy(final Thread thread)

Add a specified thread to the set of busy event dispatch threads. If this set already contains the specified thread or the thread is null, the call leaves this set unchanged and returns silently.

Parameters
thread:Thread

thread to be added to this set, if not present.

See Also
AWTAutoShutdown#notifyThreadFree, AWTAutoShutdown#isReadyToShutdown
notifyThreadFreeback to summary
public void notifyThreadFree(final Thread thread)

Remove a specified thread from the set of busy event dispatch threads. If this set doesn't contain the specified thread or the thread is null, the call leaves this set unchanged and returns silently.

Parameters
thread:Thread

thread to be removed from this set, if present.

See Also
AWTAutoShutdown#notifyThreadBusy, AWTAutoShutdown#isReadyToShutdown
notifyToolkitThreadBusyback to summary
public static void notifyToolkitThreadBusy()

Notify that the toolkit thread is not waiting for a native event to appear in its queue.

See Also
AWTAutoShutdown#notifyToolkitThreadFree, AWTAutoShutdown#setToolkitBusy, AWTAutoShutdown#isReadyToShutdown
notifyToolkitThreadFreeback to summary
public static void notifyToolkitThreadFree()

Notify that the toolkit thread is waiting for a native event to appear in its queue.

See Also
AWTAutoShutdown#notifyToolkitThreadFree, AWTAutoShutdown#setToolkitBusy, AWTAutoShutdown#isReadyToShutdown
registerPeerback to summary
pack-priv void registerPeer(final Object target, final Object peer)
runback to summary
public void run()

Implements java.lang.Runnable.run.

Implementation of the Runnable interface. Encapsulates the blocker thread functionality.

See Also
AWTAutoShutdown#isReadyToShutdown
setToolkitBusyback to summary
private void setToolkitBusy(final boolean busy)

Notify about the toolkit thread state change.

Parameters
busy:boolean

true if the toolkit thread state changes from idle to busy.

See Also
AWTAutoShutdown#notifyToolkitThreadBusy, AWTAutoShutdown#notifyToolkitThreadFree, AWTAutoShutdown#isReadyToShutdown
unregisterPeerback to summary
pack-priv void unregisterPeer(final Object target, final Object peer)