jdk/src/share/classes/sun/misc/Timer.java
author alanb
Fri, 22 Feb 2013 14:04:06 +0000
changeset 16023 58ecc1b8327b
parent 5506 202f599c92aa
permissions -rw-r--r--
8008290: (profiles) Startup regression due to additional checking of JAR file manifests Reviewed-by: lancea, chegar, iris, mchung, sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
    A Timer object is used by algorithms that require timed events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
    For example, in an animation loop, a timer would help in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    determining when to change frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    A timer has an interval which determines when it "ticks";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    that is, a timer delays for the specified interval and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    it calls the owner's tick() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    Here's an example of creating a timer with a 5 sec interval:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    class Main implements Timeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        public void tick(Timer timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            System.out.println("tick");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            (new Timer(this, 5000)).cont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    A timer can be stopped, continued, or reset at any time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    A timer's state is not stopped while it's calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    owner's tick() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    A timer can be regular or irregular.  If in regular mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    a timer ticks at the specified interval, regardless of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    how long the owner's tick() method takes.  While the timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    is running, no ticks are ever discarded.  That means that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    the owner's tick() method takes longer than the interval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    the ticks that would have occurred are delivered immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    In irregular mode, a timer starts delaying for exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    the specified interval only after the tick() method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    Synchronization issues: do not hold the timer's monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    while calling any of the Timer operations below otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    the Timer class will deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    @author     Patrick Chan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    Synchronization issues:  there are two data structures that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    require locking.  A Timer object and the Timer queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    (described in the TimerThread class).  To avoid deadlock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    the timer queue monitor is always acquired before the timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    object's monitor.  However, the timer queue monitor is acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    only if the timer operation will make use of the timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    queue, e.g. stop().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    The class monitor on the class TimerThread severs as the monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    to the timer queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    Possible feature: perhaps a timer should have an associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    thread priority.  The thread that makes the callback temporarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    takes on that priority before calling the owner's tick() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
public class Timer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * This is the owner of the timer.  Its tick method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * called when the timer ticks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public Timeable owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * This is the interval of time in ms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    long interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * This variable is used for two different purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * This is done in order to save space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * If 'stopped' is true, this variable holds the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * that the timer was stopped; otherwise, this variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * is used by the TimerThread to determine when the timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * should tick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    long sleepUntil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * This is the time remaining before the timer ticks.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * is only valid if 'stopped' is true.  If the timer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * continued, the next tick will happen remaingTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * milliseconds later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    long remainingTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * True iff the timer is in regular mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    boolean regular;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * True iff the timer has been stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    boolean stopped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /* **************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Timer queue-related variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * ************************************************************** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * A link to another timer object.  This is used while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * timer object is enqueued in the timer queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    Timer next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /* **************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Timer methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * ************************************************************** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * This variable holds a handle to the TimerThread class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * the purpose of getting at the class monitor.  The reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * why Class.forName("TimerThread") is not used is because it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * doesn't appear to work when loaded via a net class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static TimerThread timerThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Creates a timer object that is owned by 'owner' and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * with the interval 'interval' milliseconds.  The new timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * object is stopped and is regular.  getRemainingTime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * return 'interval' at this point.  getStopTime() returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * the time this object was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param owner    owner of the timer object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param interval interval of the timer in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public Timer(Timeable owner, long interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.owner = owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.interval = interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        remainingTime = interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        regular = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        sleepUntil = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        stopped = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        synchronized (getClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (timerThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                timerThread = new TimerThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns true if this timer is stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public synchronized boolean isStopped() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return stopped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Stops the timer.  The amount of time the timer has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * delayed is saved so if the timer is continued, it will only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * delay for the amount of time remaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Note that even after stopping a timer, one more tick may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * still occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * This method is MT-safe; i.e. it is synchronized but for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * implementation reasons, the synchronized modifier cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * be included in the method declaration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        synchronized (timerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (!stopped) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    TimerThread.dequeue(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    remainingTime = Math.max(0, sleepUntil - now);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    sleepUntil = now;        // stop time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    stopped = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
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
     * Continue the timer.  The next tick will come at getRemainingTime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * milliseconds later.  If the timer is not stopped, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * call will be a no-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * This method is MT-safe; i.e. it is synchronized but for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * implementation reasons, the synchronized modifier cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * be included in the method declaration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public void cont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        synchronized (timerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (stopped) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    // The TimerTickThread avoids requeuing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    // timer only if the sleepUntil value has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    // The following guarantees that the sleepUntil
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    // value will be different; without this guarantee,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    // it's theoretically possible for the timer to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    // inserted twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    sleepUntil = Math.max(sleepUntil + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        System.currentTimeMillis() + remainingTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    TimerThread.enqueue(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    stopped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Resets the timer's remaining time to the timer's interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * If the timer's running state is not altered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        synchronized (timerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                setRemainingTime(interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Returns the time at which the timer was last stopped.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * return value is valid only if the timer is stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public synchronized long getStopTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return sleepUntil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Returns the timer's interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public synchronized long getInterval() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Changes the timer's interval.  The new interval setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * does not take effect until after the next tick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * This method does not alter the remaining time or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * running state of the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param interval new interval of the timer in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public synchronized void setInterval(long interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        this.interval = interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Returns the remaining time before the timer's next tick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * The return value is valid only if timer is stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public synchronized long getRemainingTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return remainingTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Sets the remaining time before the timer's next tick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * This method does not alter the timer's running state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * This method is MT-safe; i.e. it is synchronized but for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * implementation reasons, the synchronized modifier cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * be included in the method declaration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param time new remaining time in milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public void setRemainingTime(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        synchronized (timerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (stopped) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    remainingTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    remainingTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    cont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * In regular mode, a timer ticks at the specified interval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * regardless of how long the owner's tick() method takes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * While the timer is running, no ticks are ever discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * That means that if the owner's tick() method takes longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * than the interval, the ticks that would have occurred are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * delivered immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * In irregular mode, a timer starts delaying for exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * the specified interval only after the tick() method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public synchronized void setRegular(boolean regular) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        this.regular = regular;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * This method is used only for testing purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    protected Thread getTimerThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return TimerThread.timerThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
This class implements the timer queue and is exclusively used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
Timer class.  There are only two methods exported to the Timer class -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
enqueue, for inserting a timer into queue and dequeue, for removing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
a timer from the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
A timer in the timer queue is awaiting a tick.  When a timer is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
ticked, it is removed from the timer queue before the owner's tick()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
A single timer thread manages the timer queue.  This timer thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
looks at the head of the timer queue and delays until it's time for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
the timer to tick.  When the time comes, the timer thread creates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
callback thread to call the timer owner's tick() method.  The timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
thread then processes the next timer in the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
When a timer is inserted at the head of the queue, the timer thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
notified.  This causes the timer thread to prematurely wake up and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
process the new head of the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
class TimerThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Set to true to get debugging output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * This is a handle to the thread managing the thread queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    static TimerThread timerThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * This flag is set if the timer thread has been notified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * while it was in the timed wait.  This flag allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * timer thread to tell whether or not the wait completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    static boolean notified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    protected TimerThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        super("TimerThread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        timerThread = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public synchronized void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            long delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            while (timerQueue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                   // Just drop through and check timerQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            notified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            delay = timerQueue.sleepUntil - System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (delay > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    wait(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    // Just drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            // remove from timer queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (!notified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                Timer timer = timerQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                timerQueue = timerQueue.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                TimerTickThread thr = TimerTickThread.call(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    timer, timer.sleepUntil);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    long delta = (System.currentTimeMillis() - timer.sleepUntil);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    System.out.println("tick(" + thr.getName() + ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        + timer.interval + ","+delta+ ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    if (delta > 250) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        System.out.println("*** BIG DELAY ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /* *******************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
       Timer Queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
       ******************************************************* */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * The timer queue is a queue of timers waiting to tick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    static Timer timerQueue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Uses timer.sleepUntil to determine where in the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * to insert the timer object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * A new ticker thread is created only if the timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * is inserted at the beginning of the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * The timer must not already be in the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Assumes the caller has the TimerThread monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    static protected void enqueue(Timer timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        Timer prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        Timer cur = timerQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (cur == null || timer.sleepUntil <= cur.sleepUntil) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // insert at front of queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            timer.next = timerQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            timerQueue = timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            notified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            timerThread.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                prev = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } while (cur != null && timer.sleepUntil > cur.sleepUntil);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            // insert or append to the timer queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            timer.next = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            prev.next = timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            System.out.print(Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                + ": enqueue " + timer.interval + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            cur = timerQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            while(cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                long delta = cur.sleepUntil - now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                System.out.print(cur.interval + "(" + delta + ") ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * If the timer is not in the queue, returns false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * otherwise removes the timer from the timer queue and returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Assumes the caller has the TimerThread monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    static protected boolean dequeue(Timer timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        Timer prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        Timer cur = timerQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        while (cur != null && cur != timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            prev = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (cur == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                System.out.println(Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    + ": dequeue " + timer.interval + ": no-op");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }       if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            timerQueue = timer.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            notified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            timerThread.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            prev.next = timer.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        timer.next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            System.out.print(Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                + ": dequeue " + timer.interval + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            cur = timerQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            while(cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                long delta = cur.sleepUntil - now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                System.out.print(cur.interval + "(" + delta + ") ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Inserts the timer back into the queue.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * is used by a callback thread after it has called the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * timer owner's tick() method.  This method recomputes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * the sleepUntil field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Assumes the caller has the TimerThread and Timer monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    protected static void requeue(Timer timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (!timer.stopped) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (timer.regular) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                timer.sleepUntil += timer.interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                timer.sleepUntil = now + timer.interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            enqueue(timer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        } else if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            System.out.println(Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                + ": requeue " + timer.interval + ": no-op");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
This class implements a simple thread whose only purpose is to call a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
timer owner's tick() method.  A small fixed-sized pool of threads is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
maintained and is protected by the class monitor.  If the pool is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
exhausted, a new thread is temporarily created and destroyed when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
A thread that's in the pool waits on it's own monitor.  When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
thread is retrieved from the pool, the retriever notifies the thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
class TimerTickThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Maximum size of the thread pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    static final int MAX_POOL_SIZE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Number of threads in the pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    static int curPoolSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * The pool of timer threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    static TimerTickThread pool = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Is used when linked into the thread pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    TimerTickThread next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * This is the handle to the timer whose owner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * tick() method will be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * The value of a timer's sleepUntil value is captured here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * This is used to determine whether or not the timer should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * be reinserted into the queue.  If the timer's sleepUntil
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * value has changed, the timer is not reinserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    long lastSleepUntil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Creates a new callback thread to call the timer owner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * tick() method.  A thread is taken from the pool if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * is available, otherwise, a new thread is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * The thread handle is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    protected static synchronized TimerTickThread call(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            Timer timer, long sleepUntil) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        TimerTickThread thread = pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (thread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            // create one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            thread = new TimerTickThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            thread.timer = timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            thread.lastSleepUntil = sleepUntil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            pool = pool.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            thread.timer = timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            thread.lastSleepUntil = sleepUntil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            synchronized (thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                thread.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * Returns false if the thread should simply exit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * otherwise the thread is returned the pool, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * it waits to be notified.  (I did try to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * class monitor but the time between the notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * and breaking out of the wait seemed to take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * significantly longer; need to look into this later.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    private boolean returnToPool() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        synchronized (getClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (curPoolSize >= MAX_POOL_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            next = pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            pool = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            curPoolSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            timer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        while (timer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                   // Just drop through and retest timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        synchronized (getClass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            curPoolSize--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            timer.owner.tick(timer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            synchronized (TimerThread.timerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                synchronized (timer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    if (lastSleepUntil == timer.sleepUntil) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                        TimerThread.requeue(timer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        } while (returnToPool());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
}