jdk/src/java.desktop/share/classes/javax/swing/TimerQueue.java
author psadhukhan
Mon, 16 Nov 2015 10:56:21 +0300
changeset 34397 86a74cb6c903
parent 32865 f9cb6e427f9e
child 37550 c8252b8fea3d
permissions -rw-r--r--
8081491: The case print incomplete. Reviewed-by: alexsch, rchamyal
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2486
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2486
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2486
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2486
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2486
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
    32
import java.security.AccessController;
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
    33
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.*;
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    36
import java.util.concurrent.locks.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.atomic.AtomicLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.awt.AppContext;
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
    39
import sun.misc.ManagedLocalsThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Internal class to manage all Timers using one thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * TimerQueue manages a queue of Timers. The Timers are chained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * together in a linked list sorted by the order in which they will expire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Dave Moore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Igor Kushnirskiy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
class TimerQueue implements Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final Object sharedInstanceKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        new StringBuffer("TimerQueue.sharedInstanceKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final Object expiredTimersKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        new StringBuffer("TimerQueue.expiredTimersKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final DelayQueue<DelayedTimer> queue;
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    57
    private volatile boolean running;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    58
    private final Lock runningLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /* Lock object used in place of class object for synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * (4187686)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final Object classLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /** Base of nanosecond timings, to avoid wrapping */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final long NANO_ORIGIN = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Constructor for TimerQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public TimerQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        queue = new DelayQueue<DelayedTimer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // Now start the TimerQueue thread.
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    75
        runningLock = new ReentrantLock();
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    76
        startIfNeeded();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static TimerQueue sharedInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        synchronized (classLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            TimerQueue sharedInst = (TimerQueue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                    SwingUtilities.appContextGet(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                                        sharedInstanceKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (sharedInst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                sharedInst = new TimerQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                SwingUtilities.appContextPut(sharedInstanceKey, sharedInst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return sharedInst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    94
    void startIfNeeded() {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    95
        if (! running) {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    96
            runningLock.lock();
32120
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
    97
            if (running) {
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
    98
                return;
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
    99
            }
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   100
            try {
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   101
                final ThreadGroup threadGroup = AppContext.getAppContext().getThreadGroup();
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   102
                AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   103
                    String name = "TimerQueue";
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
   104
                    Thread timerThread = new ManagedLocalsThread(threadGroup,
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
   105
                                                                 this, name);
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   106
                    timerThread.setDaemon(true);
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   107
                    timerThread.setPriority(Thread.NORM_PRIORITY);
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   108
                    timerThread.start();
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   109
                    return null;
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   110
                });
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   111
                running = true;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   112
            } finally {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   113
                runningLock.unlock();
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   114
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    void addTimer(Timer timer, long delayMillis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // If the Timer is already in the queue, then ignore the add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (! containsTimer(timer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                addTimer(new DelayedTimer(timer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                      TimeUnit.MILLISECONDS.toNanos(delayMillis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                      + now()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private void addTimer(DelayedTimer delayedTimer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        assert delayedTimer != null && ! containsTimer(delayedTimer.getTimer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Timer timer = delayedTimer.getTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            timer.delayedTimer = delayedTimer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            queue.add(delayedTimer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    void removeTimer(Timer timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (timer.delayedTimer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                queue.remove(timer.delayedTimer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                timer.delayedTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    boolean containsTimer(Timer timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return timer.delayedTimer != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public void run() {
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   168
        runningLock.lock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            while (running) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                try {
32120
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   172
                    DelayedTimer runningTimer = queue.take();
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   173
                    Timer timer = runningTimer.getTimer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        DelayedTimer delayedTimer = timer.delayedTimer;
32120
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   177
                        if (delayedTimer == runningTimer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                            /*
32120
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   179
                             * Timer is not removed (delayedTimer != null)
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   180
                             * or not removed and added (runningTimer == delayedTimer)
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   181
                             * after we get it from the queue and before the
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   182
                             * lock on the timer is acquired
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            timer.post(); // have timer post an event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            timer.delayedTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            if (timer.isRepeats()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                delayedTimer.setTime(now()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                    + TimeUnit.MILLISECONDS.toNanos(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                          timer.getDelay()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                addTimer(delayedTimer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        }
13352
a9ef04068234 7167780: Hang javasoft.sqe.tests.api.javax.swing.Timer.Ctor2Tests
rupashka
parents: 5506
diff changeset
   193
a9ef04068234 7167780: Hang javasoft.sqe.tests.api.javax.swing.Timer.Ctor2Tests
rupashka
parents: 5506
diff changeset
   194
                        // Allow run other threads on systems without kernel threads
a9ef04068234 7167780: Hang javasoft.sqe.tests.api.javax.swing.Timer.Ctor2Tests
rupashka
parents: 5506
diff changeset
   195
                        timer.getLock().newCondition().awaitNanos(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    } catch (SecurityException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    }
2485
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   200
                } catch (InterruptedException ie) {
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   201
                    // Shouldn't ignore InterruptedExceptions here, so AppContext
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   202
                    // is disposed gracefully, see 6799345 for details
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   203
                    if (AppContext.getAppContext().isDisposed()) {
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   204
                        break;
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   205
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        catch (ThreadDeath td) {
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   210
            // Mark all the timers we contain as not being queued.
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   211
            for (DelayedTimer delayedTimer : queue) {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   212
                delayedTimer.getTimer().cancelEvent();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   214
            throw td;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   215
        } finally {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   216
            running = false;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   217
            runningLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        buf.append("TimerQueue (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        boolean isFirst = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        for (DelayedTimer delayedTimer : queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (! isFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            buf.append(delayedTimer.getTimer().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            isFirst = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        buf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Returns nanosecond time offset by origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1273
diff changeset
   240
    private static long now() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return System.nanoTime() - NANO_ORIGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    static class DelayedTimer implements Delayed {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // most of it copied from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // java.util.concurrent.ScheduledThreadPoolExecutor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * Sequence number to break scheduling ties, and in turn to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * guarantee FIFO order among tied entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        private static final AtomicLong sequencer = new AtomicLong(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        /** Sequence number to break ties FIFO */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        private final long sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        /** The time the task is enabled to execute in nanoTime units */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        private volatile long time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        private final Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        DelayedTimer(Timer timer, long nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            this.timer = timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            time = nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            sequenceNumber = sequencer.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 32120
diff changeset
   270
        public final long getDelay(TimeUnit unit) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            return  unit.convert(time - now(), TimeUnit.NANOSECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        final void setTime(long nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            time = nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        final Timer getTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        public int compareTo(Delayed other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (other == this) { // compare zero ONLY if same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (other instanceof DelayedTimer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                DelayedTimer x = (DelayedTimer)other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                long diff = time - x.time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (diff < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                } else if (diff > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                } else if (sequenceNumber < x.sequenceNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            long d = (getDelay(TimeUnit.NANOSECONDS) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                      other.getDelay(TimeUnit.NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return (d == 0) ? 0 : ((d < 0) ? -1 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
}