jdk/src/share/classes/javax/swing/TimerQueue.java
author idk
Mon, 23 Jun 2008 15:21:37 -0400
changeset 1273 3b2eba521268
parent 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
6623943: javax.swing.TimerQueue's thread occasionally fails to start Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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(
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   100
                    new java.security.PrivilegedAction() {
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
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    } catch (SecurityException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        timer.getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                } catch (InterruptedException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        catch (ThreadDeath td) {
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   199
            // 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
   200
            for (DelayedTimer delayedTimer : queue) {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   201
                delayedTimer.getTimer().cancelEvent();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
1273
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   203
            throw td;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   204
        } finally {
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   205
            running = false;
3b2eba521268 6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents: 2
diff changeset
   206
            runningLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        buf.append("TimerQueue (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        boolean isFirst = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        for (DelayedTimer delayedTimer : queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (! isFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            buf.append(delayedTimer.getTimer().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            isFirst = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        buf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Returns nanosecond time offset by origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    private final static long now() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return System.nanoTime() - NANO_ORIGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    static class DelayedTimer implements Delayed {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // most of it copied from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // java.util.concurrent.ScheduledThreadPoolExecutor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * Sequence number to break scheduling ties, and in turn to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * guarantee FIFO order among tied entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        private static final AtomicLong sequencer = new AtomicLong(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        /** Sequence number to break ties FIFO */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        private final long sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        /** The time the task is enabled to execute in nanoTime units */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        private volatile long time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        private final Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        DelayedTimer(Timer timer, long nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            this.timer = timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            time = nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            sequenceNumber = sequencer.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        final public long getDelay(TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return  unit.convert(time - now(), TimeUnit.NANOSECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        final void setTime(long nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            time = nanos;
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 Timer getTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public int compareTo(Delayed other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (other == this) { // compare zero ONLY if same object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (other instanceof DelayedTimer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                DelayedTimer x = (DelayedTimer)other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                long diff = time - x.time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                if (diff < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                } else if (diff > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                } else if (sequenceNumber < x.sequenceNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            long d = (getDelay(TimeUnit.NANOSECONDS) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                      other.getDelay(TimeUnit.NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return (d == 0) ? 0 : ((d < 0) ? -1 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
}