jdk/src/share/classes/javax/swing/TimerQueue.java
author dmarkov
Wed, 16 Apr 2014 12:51:25 +0400
changeset 24184 4da2f6ec4dab
parent 23010 6dadb192ad81
permissions -rw-r--r--
8032874: ArrayIndexOutOfBoundsException in JTable while clearing data in JTable Reviewed-by: alexp, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 13352
diff changeset
     2
 * Copyright (c) 1997, 2012, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.*;
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    34
import java.util.concurrent.locks.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.atomic.AtomicLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
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();
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    96
            try {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    97
                final ThreadGroup threadGroup =
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    98
                    AppContext.getAppContext().getThreadGroup();
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
    99
                java.security.AccessController.doPrivileged(
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1273
diff changeset
   100
                    new java.security.PrivilegedAction<Object>() {
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   101
                    public Object run() {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   102
                        Thread timerThread = new Thread(threadGroup, TimerQueue.this,
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   103
                                                        "TimerQueue");
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   104
                        timerThread.setDaemon(true);
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   105
                        timerThread.setPriority(Thread.NORM_PRIORITY);
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   106
                        timerThread.start();
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   107
                        return null;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   108
                    }
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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    Timer timer = queue.take().getTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    timer.getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        DelayedTimer delayedTimer = timer.delayedTimer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        if (delayedTimer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                             * Timer is not removed after we get it from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                             * the queue and before the lock on the timer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                             * acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            timer.post(); // have timer post an event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            timer.delayedTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            if (timer.isRepeats()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                delayedTimer.setTime(now()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                    + TimeUnit.MILLISECONDS.toNanos(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                          timer.getDelay()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                addTimer(delayedTimer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        }
13352
a9ef04068234 7167780: Hang javasoft.sqe.tests.api.javax.swing.Timer.Ctor2Tests
rupashka
parents: 5506
diff changeset
   190
a9ef04068234 7167780: Hang javasoft.sqe.tests.api.javax.swing.Timer.Ctor2Tests
rupashka
parents: 5506
diff changeset
   191
                        // 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
   192
                        timer.getLock().newCondition().awaitNanos(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    } catch (SecurityException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    }
2485
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   197
                } catch (InterruptedException ie) {
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   198
                    // 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
   199
                    // 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
   200
                    if (AppContext.getAppContext().isDisposed()) {
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   201
                        break;
dc7165666847 6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents: 1301
diff changeset
   202
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        catch (ThreadDeath td) {
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   207
            // 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
   208
            for (DelayedTimer delayedTimer : queue) {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   209
                delayedTimer.getTimer().cancelEvent();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   211
            throw td;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   212
        } finally {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   213
            running = false;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   214
            runningLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        buf.append("TimerQueue (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        boolean isFirst = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        for (DelayedTimer delayedTimer : queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (! isFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            buf.append(delayedTimer.getTimer().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            isFirst = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        buf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Returns nanosecond time offset by origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1273
diff changeset
   237
    private static long now() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return System.nanoTime() - NANO_ORIGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    static class DelayedTimer implements Delayed {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // most of it copied from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // java.util.concurrent.ScheduledThreadPoolExecutor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * Sequence number to break scheduling ties, and in turn to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * guarantee FIFO order among tied entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        private static final AtomicLong sequencer = new AtomicLong(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        /** Sequence number to break ties FIFO */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        private final long sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        /** The time the task is enabled to execute in nanoTime units */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        private volatile long time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        private final Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        DelayedTimer(Timer timer, long nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            this.timer = timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            time = nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            sequenceNumber = sequencer.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        final public long getDelay(TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return  unit.convert(time - now(), TimeUnit.NANOSECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        final void setTime(long nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            time = nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        final Timer getTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        public int compareTo(Delayed other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (other == this) { // compare zero ONLY if same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (other instanceof DelayedTimer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                DelayedTimer x = (DelayedTimer)other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                long diff = time - x.time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (diff < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                } else if (diff > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                } else if (sequenceNumber < x.sequenceNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            long d = (getDelay(TimeUnit.NANOSECONDS) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                      other.getDelay(TimeUnit.NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            return (d == 0) ? 0 : ((d < 0) ? -1 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
}