/*
* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.awt;
import java.awt.AWTEvent;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import 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:
*
*
peerMap - the map between the existing peer objects
* and their associated targets
*
toolkitThreadBusy - whether the toolkit thread
* is waiting for a new native event to appear in its queue
* or is dispatching an event
*
busyThreadSet - a set of 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.
*
* 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.
*/
public final class AWTAutoShutdown implements Runnable {
private static final AWTAutoShutdown theInstance = new AWTAutoShutdown();
/**
* This lock object is used to synchronize shutdown operations.
*/
private final Object mainLock = new Object();
/**
* 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 final Object activationLock = new Object();
/**
* 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.
*/
private final Set busyThreadSet = new HashSet<>(7);
/**
* Indicates whether the toolkit thread is waiting for a new native
* event to appear or is dispatching an event.
*/
private boolean toolkitThreadBusy = false;
/**
* This is a map between components and their peers.
* we should work with in under activationLock&mainLock lock.
*/
private final Map