src/share/classes/javax/swing/TimerQueue.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 
  28 package javax.swing;
  29 
  30 
  31 
  32 import java.util.*;
  33 import java.util.concurrent.*;
  34 import java.util.concurrent.locks.*;
  35 import java.util.concurrent.atomic.AtomicLong;


  36 import sun.awt.AppContext;
  37 
  38 
  39 
  40 /**
  41  * Internal class to manage all Timers using one thread.
  42  * TimerQueue manages a queue of Timers. The Timers are chained
  43  * together in a linked list sorted by the order in which they will expire.
  44  *
  45  * @author Dave Moore
  46  * @author Igor Kushnirskiy
  47  */
  48 class TimerQueue implements Runnable
  49 {
  50     private static final Object sharedInstanceKey =
  51         new StringBuffer("TimerQueue.sharedInstanceKey");
  52     private static final Object expiredTimersKey =
  53         new StringBuffer("TimerQueue.expiredTimersKey");
  54 
  55     private final DelayQueue<DelayedTimer> queue;


 200                     if (AppContext.getAppContext().isDisposed()) {
 201                         break;
 202                     }
 203                 }
 204             }
 205         }
 206         catch (ThreadDeath td) {
 207             // Mark all the timers we contain as not being queued.
 208             for (DelayedTimer delayedTimer : queue) {
 209                 delayedTimer.getTimer().cancelEvent();
 210             }
 211             throw td;
 212         } finally {
 213             running = false;
 214             runningLock.unlock();
 215         }
 216     }
 217 
 218 
 219     public String toString() {
 220         StringBuilder buf = new StringBuilder();
 221         buf.append("TimerQueue (");
 222         boolean isFirst = true;
 223         for (DelayedTimer delayedTimer : queue) {
 224             if (! isFirst) {
 225                 buf.append(", ");
 226             }
 227             buf.append(delayedTimer.getTimer().toString());
 228             isFirst = false;
 229         }
 230         buf.append(")");
 231         return buf.toString();
 232     }
 233 
 234     /**
 235      * Returns nanosecond time offset by origin
 236      */
 237     private static long now() {
 238         return System.nanoTime() - NANO_ORIGIN;
 239     }
 240 
 241     static class DelayedTimer implements Delayed {
 242         // most of it copied from
 243         // java.util.concurrent.ScheduledThreadPoolExecutor
 244 
 245         /**
 246          * Sequence number to break scheduling ties, and in turn to
 247          * guarantee FIFO order among tied entries.
 248          */
 249         private static final AtomicLong sequencer = new AtomicLong(0);
 250 
 251         /** Sequence number to break ties FIFO */


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 
  28 package javax.swing;
  29 
  30 
  31 
  32 import java.util.*;
  33 import java.util.concurrent.*;
  34 import java.util.concurrent.locks.*;
  35 import java.util.concurrent.atomic.AtomicLong;
  36 import java.util.stream.Collectors;
  37 
  38 import sun.awt.AppContext;
  39 
  40 
  41 
  42 /**
  43  * Internal class to manage all Timers using one thread.
  44  * TimerQueue manages a queue of Timers. The Timers are chained
  45  * together in a linked list sorted by the order in which they will expire.
  46  *
  47  * @author Dave Moore
  48  * @author Igor Kushnirskiy
  49  */
  50 class TimerQueue implements Runnable
  51 {
  52     private static final Object sharedInstanceKey =
  53         new StringBuffer("TimerQueue.sharedInstanceKey");
  54     private static final Object expiredTimersKey =
  55         new StringBuffer("TimerQueue.expiredTimersKey");
  56 
  57     private final DelayQueue<DelayedTimer> queue;


 202                     if (AppContext.getAppContext().isDisposed()) {
 203                         break;
 204                     }
 205                 }
 206             }
 207         }
 208         catch (ThreadDeath td) {
 209             // Mark all the timers we contain as not being queued.
 210             for (DelayedTimer delayedTimer : queue) {
 211                 delayedTimer.getTimer().cancelEvent();
 212             }
 213             throw td;
 214         } finally {
 215             running = false;
 216             runningLock.unlock();
 217         }
 218     }
 219 
 220 
 221     public String toString() {
 222         return queue.stream()
 223                 .map(delayedTimer -> delayedTimer.getTimer().toString())
 224                 .collect(Collectors.joining(", ", "TimerQueue (", ")"));









 225     }
 226 
 227     /**
 228      * Returns nanosecond time offset by origin
 229      */
 230     private static long now() {
 231         return System.nanoTime() - NANO_ORIGIN;
 232     }
 233 
 234     static class DelayedTimer implements Delayed {
 235         // most of it copied from
 236         // java.util.concurrent.ScheduledThreadPoolExecutor
 237 
 238         /**
 239          * Sequence number to break scheduling ties, and in turn to
 240          * guarantee FIFO order among tied entries.
 241          */
 242         private static final AtomicLong sequencer = new AtomicLong(0);
 243 
 244         /** Sequence number to break ties FIFO */