Class Timers


  • public class Timers
    extends java.lang.Object
    This class supports the "setTimeout" and "clearTimeout" methods of semi-standard JavaScript. It does it within a single thread by keeping track of a queue of timeout objects, and then it blocks the thread. It's used solely within the Shell right now.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Timers.Timeout
      An object to go on the priority queue.
    • Constructor Summary

      Constructors 
      Constructor Description
      Timers()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.lang.Object clearTimeout​(java.lang.Object[] args)  
      private boolean executeNext​(Context cx, Scriptable scope)
      Put up to one task on the context's "microtask queue." If the next task is not ready to run for some time, then block the calling thread until the time is up.
      void install​(Scriptable scope)
      Initialize the "setTimeout" and "clearTimeout" functions on the specified scope.
      void runAllTimers​(Context cx, Scriptable scope)
      Execute all pending timers and microtasks, blocking the thread if we need to wait for any timers to time out.
      private java.lang.Object setTimeout​(java.lang.Object[] args)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • lastId

        private int lastId
      • timers

        private final java.util.HashMap<java.lang.Integer,​Timers.Timeout> timers
      • timerQueue

        private final java.util.PriorityQueue<Timers.Timeout> timerQueue
    • Constructor Detail

      • Timers

        public Timers()
    • Method Detail

      • install

        public void install​(Scriptable scope)
        Initialize the "setTimeout" and "clearTimeout" functions on the specified scope.
        Parameters:
        scope - the scope where the functions should be defined
      • runAllTimers

        public void runAllTimers​(Context cx,
                                 Scriptable scope)
                          throws java.lang.InterruptedException
        Execute all pending timers and microtasks, blocking the thread if we need to wait for any timers to time out.
        Parameters:
        cx - The Context to use to execute microtasks and timer functions
        scope - the global scope
        Throws:
        java.lang.InterruptedException - if the thread is interrupted while sleeping
      • executeNext

        private boolean executeNext​(Context cx,
                                    Scriptable scope)
                             throws java.lang.InterruptedException
        Put up to one task on the context's "microtask queue." If the next task is not ready to run for some time, then block the calling thread until the time is up.
        Parameters:
        cx - the context
        scope - the current scope
        Returns:
        true if something was placed on the queue, and false if the queue is empty
        Throws:
        java.lang.InterruptedException - if the thread was interrupted
      • setTimeout

        private java.lang.Object setTimeout​(java.lang.Object[] args)
      • clearTimeout

        private java.lang.Object clearTimeout​(java.lang.Object[] args)