jdk/src/java.desktop/share/classes/javax/swing/TimerQueue.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 37550 c8252b8fea3d
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@oracle.com
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Internal class to manage all Timers using one thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * TimerQueue manages a queue of Timers. The Timers are chained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * together in a linked list sorted by the order in which they will expire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Dave Moore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Igor Kushnirskiy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
class TimerQueue implements Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final Object sharedInstanceKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        new StringBuffer("TimerQueue.sharedInstanceKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final Object expiredTimersKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        new StringBuffer("TimerQueue.expiredTimersKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final DelayQueue<DelayedTimer> queue;
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    56
    private volatile boolean running;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    57
    private final Lock runningLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* Lock object used in place of class object for synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * (4187686)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final Object classLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /** Base of nanosecond timings, to avoid wrapping */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final long NANO_ORIGIN = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Constructor for TimerQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public TimerQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        queue = new DelayQueue<DelayedTimer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // Now start the TimerQueue thread.
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    74
        runningLock = new ReentrantLock();
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    75
        startIfNeeded();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static TimerQueue sharedInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        synchronized (classLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            TimerQueue sharedInst = (TimerQueue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                    SwingUtilities.appContextGet(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                                        sharedInstanceKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (sharedInst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                sharedInst = new TimerQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                SwingUtilities.appContextPut(sharedInstanceKey, sharedInst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return sharedInst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    93
    void startIfNeeded() {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    94
        if (! running) {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    95
            runningLock.lock();
32120
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
    96
            if (running) {
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
    97
                return;
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
    98
            }
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    99
            try {
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   100
                final ThreadGroup threadGroup = AppContext.getAppContext().getThreadGroup();
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   101
                AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   102
                    String name = "TimerQueue";
37550
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 32865
diff changeset
   103
                    Thread timerThread =
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 32865
diff changeset
   104
                        new Thread(threadGroup, this, name, 0, false);
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   105
                    timerThread.setDaemon(true);
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   106
                    timerThread.setPriority(Thread.NORM_PRIORITY);
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   107
                    timerThread.start();
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
   108
                    return null;
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   109
                });
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   110
                running = true;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   111
            } finally {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   112
                runningLock.unlock();
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   113
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    void addTimer(Timer timer, long delayMillis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // If the Timer is already in the queue, then ignore the add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (! containsTimer(timer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                addTimer(new DelayedTimer(timer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                      TimeUnit.MILLISECONDS.toNanos(delayMillis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                      + now()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private void addTimer(DelayedTimer delayedTimer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        assert delayedTimer != null && ! containsTimer(delayedTimer.getTimer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Timer timer = delayedTimer.getTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            timer.delayedTimer = delayedTimer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            queue.add(delayedTimer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    void removeTimer(Timer timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (timer.delayedTimer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                queue.remove(timer.delayedTimer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                timer.delayedTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    boolean containsTimer(Timer timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return timer.delayedTimer != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
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
    public void run() {
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   167
        runningLock.lock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            while (running) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                try {
32120
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   171
                    DelayedTimer runningTimer = queue.take();
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   172
                    Timer timer = runningTimer.getTimer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        DelayedTimer delayedTimer = timer.delayedTimer;
32120
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   176
                        if (delayedTimer == runningTimer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                            /*
32120
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   178
                             * Timer is not removed (delayedTimer != null)
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   179
                             * or not removed and added (runningTimer == delayedTimer)
06c83c5f2912 8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents: 31653
diff changeset
   180
                             * 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
   181
                             * lock on the timer is acquired
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            timer.post(); // have timer post an event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            timer.delayedTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            if (timer.isRepeats()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                delayedTimer.setTime(now()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                    + TimeUnit.MILLISECONDS.toNanos(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                          timer.getDelay()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                addTimer(delayedTimer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        }
13352
a9ef04068234 7167780: Hang javasoft.sqe.tests.api.javax.swing.Timer.Ctor2Tests
rupashka
parents: 5506
diff changeset
   192
a9ef04068234 7167780: Hang javasoft.sqe.tests.api.javax.swing.Timer.Ctor2Tests
rupashka
parents: 5506
diff changeset
   193
                        // 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
   194
                        timer.getLock().newCondition().awaitNanos(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    } catch (SecurityException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    }
2485
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   199
                } catch (InterruptedException ie) {
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   200
                    // 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
   201
                    // 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
   202
                    if (AppContext.getAppContext().isDisposed()) {
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   203
                        break;
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   204
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        catch (ThreadDeath td) {
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   209
            // 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
   210
            for (DelayedTimer delayedTimer : queue) {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   211
                delayedTimer.getTimer().cancelEvent();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   213
            throw td;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   214
        } finally {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   215
            running = false;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   216
            runningLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
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
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        buf.append("TimerQueue (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        boolean isFirst = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        for (DelayedTimer delayedTimer : queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (! isFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            buf.append(delayedTimer.getTimer().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            isFirst = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        buf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Returns nanosecond time offset by origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1273
diff changeset
   239
    private static long now() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return System.nanoTime() - NANO_ORIGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    static class DelayedTimer implements Delayed {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // most of it copied from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // java.util.concurrent.ScheduledThreadPoolExecutor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * Sequence number to break scheduling ties, and in turn to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * guarantee FIFO order among tied entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        private static final AtomicLong sequencer = new AtomicLong(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        /** Sequence number to break ties FIFO */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        private final long sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        /** The time the task is enabled to execute in nanoTime units */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        private volatile long time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        private final Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        DelayedTimer(Timer timer, long nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            this.timer = timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            time = nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            sequenceNumber = sequencer.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 32120
diff changeset
   269
        public final long getDelay(TimeUnit unit) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return  unit.convert(time - now(), TimeUnit.NANOSECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        final void setTime(long nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            time = nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        final Timer getTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        public int compareTo(Delayed other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (other == this) { // compare zero ONLY if same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (other instanceof DelayedTimer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                DelayedTimer x = (DelayedTimer)other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                long diff = time - x.time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                if (diff < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                } else if (diff > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                } else if (sequenceNumber < x.sequenceNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            long d = (getDelay(TimeUnit.NANOSECONDS) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                      other.getDelay(TimeUnit.NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return (d == 0) ? 0 : ((d < 0) ? -1 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
}