META-INF.modules.java.desktop.classes.javax.swing.TimerQueue Maven / Gradle / Ivy
/*
* Copyright (c) 1997, 2015, 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 javax.swing;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
import java.util.concurrent.atomic.AtomicLong;
import sun.awt.AppContext;
/**
* Internal class to manage all Timers using one thread.
* TimerQueue manages a queue of Timers. The Timers are chained
* together in a linked list sorted by the order in which they will expire.
*
* @author Dave Moore
* @author Igor Kushnirskiy
*/
class TimerQueue implements Runnable
{
private static final Object sharedInstanceKey =
new StringBuffer("TimerQueue.sharedInstanceKey");
private static final Object expiredTimersKey =
new StringBuffer("TimerQueue.expiredTimersKey");
private final DelayQueue queue;
private volatile boolean running;
private final Lock runningLock;
/* Lock object used in place of class object for synchronization.
* (4187686)
*/
private static final Object classLock = new Object();
/** Base of nanosecond timings, to avoid wrapping */
private static final long NANO_ORIGIN = System.nanoTime();
/**
* Constructor for TimerQueue.
*/
public TimerQueue() {
super();
queue = new DelayQueue();
// Now start the TimerQueue thread.
runningLock = new ReentrantLock();
startIfNeeded();
}
public static TimerQueue sharedInstance() {
synchronized (classLock) {
TimerQueue sharedInst = (TimerQueue)
SwingUtilities.appContextGet(
sharedInstanceKey);
if (sharedInst == null) {
sharedInst = new TimerQueue();
SwingUtilities.appContextPut(sharedInstanceKey, sharedInst);
}
return sharedInst;
}
}
void startIfNeeded() {
if (! running) {
runningLock.lock();
if (running) {
return;
}
try {
final ThreadGroup threadGroup = AppContext.getAppContext().getThreadGroup();
AccessController.doPrivileged((PrivilegedAction © 2015 - 2025 Weber Informatics LLC | Privacy Policy