Пример #1
0
        private void downHeap(int pos)
        {
            int current = pos;
            int child   = 2 * current + 1;

            while (child < size && size > 0)
            {
                // compare the children if they exist
                if (child + 1 < size &&
                    timers[child + 1].when < timers[child].when)
                {
                    child++;
                }

                // compare selected child with parent
                if (timers[current].when < timers[child].when)
                {
                    break;
                }

                // swap the two
                TimerTask tmp = timers[current];
                timers[current] = timers[child];
                timers[child]   = tmp;

                // update pos and current
                current = child;
                child   = 2 * current + 1;
            }
        }
Пример #2
0
 /**
  * Schedule a task for single execution after a specified delay.
  *
  * @param task
  *            the task to schedule.
  * @param delay
  *            amount of time in milliseconds before execution.
  * @throws IllegalArgumentException
  *                if {@code delay &lt; 0}.
  * @throws IllegalStateException
  *                if the {@code Timer} has been canceled, or if the task has been
  *                scheduled or canceled.
  */
 public void schedule(TimerTask task, long delay)
 {
     if (delay < 0)
     {
         throw new java.lang.IllegalArgumentException();
     }
     scheduleImpl(task, delay, -1, false);
 }
Пример #3
0
 /**
  * Schedule a task for repeated fixed-rate execution after a specific delay
  * has passed.
  *
  * @param task
  *            the task to schedule.
  * @param delay
  *            amount of time in milliseconds before first execution.
  * @param period
  *            amount of time in milliseconds between subsequent executions.
  * @throws IllegalArgumentException
  *                if {@code delay &lt; 0} or {@code period &lt; 0}.
  * @throws IllegalStateException
  *                if the {@code Timer} has been canceled, or if the task has been
  *                scheduled or canceled.
  */
 public void scheduleAtFixedRate(TimerTask task, long delay, long period)
 {
     if (delay < 0 || period <= 0)
     {
         throw new java.lang.IllegalArgumentException();
     }
     scheduleImpl(task, delay, period, true);
 }
Пример #4
0
        /**
         * Schedule a task for repeated fixed-rate execution after a specific time
         * has been reached.
         *
         * @param task
         *            the task to schedule.
         * @param when
         *            time of first execution.
         * @param period
         *            amount of time in milliseconds between subsequent executions.
         * @throws IllegalArgumentException
         *                if {@code when.getTime() &lt; 0} or {@code period &lt; 0}.
         * @throws IllegalStateException
         *                if the {@code Timer} has been canceled, or if the task has been
         *                scheduled or canceled.
         */
        public void scheduleAtFixedRate(TimerTask task, Date when, long period)
        {
            if (period <= 0 || when.getTime() < 0)
            {
                throw new java.lang.IllegalArgumentException();
            }
            long delay = when.getTime() - java.lang.SystemJ.currentTimeMillis();

            scheduleImpl(task, delay, period, true);
        }
Пример #5
0
        /**
         * Schedule a task for single execution. If {@code when} is less than the
         * current time, it will be scheduled to be executed as soon as possible.
         *
         * @param task
         *            the task to schedule.
         * @param when
         *            time of execution.
         * @throws IllegalArgumentException
         *                if {@code when.getTime() &lt; 0}.
         * @throws IllegalStateException
         *                if the {@code Timer} has been canceled, or if the task has been
         *                scheduled or canceled.
         */
        public void schedule(TimerTask task, Date when)
        {
            if (when.getTime() < 0)
            {
                throw new java.lang.IllegalArgumentException();
            }
            long delay = when.getTime() - java.lang.SystemJ.currentTimeMillis();

            scheduleImpl(task, delay < 0 ? 0 : delay, -1, false);
        }
Пример #6
0
 public void insert(TimerTask task)
 {
     if (timers.Length == size)
     {
         TimerTask[] appendedTimers = new TimerTask[size * 2];
         java.lang.SystemJ.arraycopy(timers, 0, appendedTimers, 0, size);
         timers = appendedTimers;
     }
     timers[size++] = task;
     upHeap();
 }
Пример #7
0
 internal int getTask(TimerTask task)
 {
     for (int i = 0; i < timers.Length; i++)
     {
         if (timers[i] == task)
         {
             return(i);
         }
     }
     return(-1);
 }
Пример #8
0
        private void upHeap()
        {
            int current = size - 1;
            int parent  = (current - 1) / 2;

            while (timers[current].when < timers[parent].when)
            {
                // swap the two
                TimerTask tmp = timers[current];
                timers[current] = timers[parent];
                timers[parent]  = tmp;

                // update pos and current
                current = parent;
                parent  = (current - 1) / 2;
            }
        }
Пример #9
0
        /*
         * Schedule a task.
         */
        private void scheduleImpl(TimerTask task, long delay, long period,
                                  bool fixedJ)
        {
            lock (impl) {
                if (impl.cancelled)
                {
                    throw new java.lang.IllegalStateException("Timer was cancelled"); //$NON-NLS-1$
                }

                long when = delay + java.lang.SystemJ.currentTimeMillis();

                if (when < 0)
                {
                    throw new java.lang.IllegalArgumentException("Illegal delay to start the TimerTask"); //$NON-NLS-1$
                }

                lock (task.lockJ) {
                    if (task.isScheduled())
                    {
                        throw new java.lang.IllegalStateException("TimerTask is scheduled already"); //$NON-NLS-1$
                    }

                    if (task.cancelled)
                    {
                        throw new java.lang.IllegalStateException("TimerTask is cancelled"); //$NON-NLS-1$
                    }

                    task.when      = when;
                    task.period    = period;
                    task.fixedRate = fixedJ;
                }

                // insert the newTask into queue
                impl.insertTask(task);
            }
        }
Пример #10
0
 internal void insertTask(TimerTask newTask)
 {
     // callers are synchronized
     tasks.insert(newTask);
     this.notify();
 }
Пример #11
0
 internal int getTask(TimerTask task)
 {
     for (int i = 0; i < timers.Length; i++)
     {
         if (timers[i] == task)
         {
             return i;
         }
     }
     return -1;
 }
Пример #12
0
 public void insert(TimerTask task)
 {
     if (timers.Length == size)
     {
         TimerTask[] appendedTimers = new TimerTask[size * 2];
         java.lang.SystemJ.arraycopy(timers, 0, appendedTimers, 0, size);
         timers = appendedTimers;
     }
     timers[size++] = task;
     upHeap();
 }
Пример #13
0
        /*
            * Schedule a task.
            */
        private void scheduleImpl(TimerTask task, long delay, long period,
                bool fixedJ)
        {
            lock (impl) {
                if (impl.cancelled) {
                    throw new java.lang.IllegalStateException("Timer was cancelled"); //$NON-NLS-1$
                }

                long when = delay + java.lang.SystemJ.currentTimeMillis();

                if (when < 0) {
                    throw new java.lang.IllegalArgumentException("Illegal delay to start the TimerTask"); //$NON-NLS-1$
                }

                lock (task.lockJ) {
                    if (task.isScheduled()) {
                        throw new java.lang.IllegalStateException("TimerTask is scheduled already"); //$NON-NLS-1$
                    }

                    if (task.cancelled) {
                        throw new java.lang.IllegalStateException("TimerTask is cancelled"); //$NON-NLS-1$
                    }

                    task.when = when;
                    task.period = period;
                    task.fixedRate = fixedJ;
                }

                // insert the newTask into queue
                impl.insertTask(task);
            }
        }
Пример #14
0
 /**
     * Schedule a task for repeated fixed-rate execution after a specific time
     * has been reached.
     *
     * @param task
     *            the task to schedule.
     * @param when
     *            time of first execution.
     * @param period
     *            amount of time in milliseconds between subsequent executions.
     * @throws IllegalArgumentException
     *                if {@code when.getTime() < 0} or {@code period < 0}.
     * @throws IllegalStateException
     *                if the {@code Timer} has been canceled, or if the task has been
     *                scheduled or canceled.
     */
 public void scheduleAtFixedRate(TimerTask task, Date when, long period)
 {
     if (period <= 0 || when.getTime() < 0) {
         throw new java.lang.IllegalArgumentException();
     }
     long delay = when.getTime() - java.lang.SystemJ.currentTimeMillis();
     scheduleImpl(task, delay, period, true);
 }
Пример #15
0
 /**
     * Schedule a task for repeated fixed-rate execution after a specific delay
     * has passed.
     *
     * @param task
     *            the task to schedule.
     * @param delay
     *            amount of time in milliseconds before first execution.
     * @param period
     *            amount of time in milliseconds between subsequent executions.
     * @throws IllegalArgumentException
     *                if {@code delay < 0} or {@code period < 0}.
     * @throws IllegalStateException
     *                if the {@code Timer} has been canceled, or if the task has been
     *                scheduled or canceled.
     */
 public void scheduleAtFixedRate(TimerTask task, long delay, long period)
 {
     if (delay < 0 || period <= 0) {
         throw new java.lang.IllegalArgumentException();
     }
     scheduleImpl(task, delay, period, true);
 }
Пример #16
0
 /**
     * Schedule a task for single execution after a specified delay.
     *
     * @param task
     *            the task to schedule.
     * @param delay
     *            amount of time in milliseconds before execution.
     * @throws IllegalArgumentException
     *                if {@code delay < 0}.
     * @throws IllegalStateException
     *                if the {@code Timer} has been canceled, or if the task has been
     *                scheduled or canceled.
     */
 public void schedule(TimerTask task, long delay)
 {
     if (delay < 0) {
         throw new java.lang.IllegalArgumentException();
     }
     scheduleImpl(task, delay, -1, false);
 }
Пример #17
0
 /**
     * Schedule a task for single execution. If {@code when} is less than the
     * current time, it will be scheduled to be executed as soon as possible.
     *
     * @param task
     *            the task to schedule.
     * @param when
     *            time of execution.
     * @throws IllegalArgumentException
     *                if {@code when.getTime() < 0}.
     * @throws IllegalStateException
     *                if the {@code Timer} has been canceled, or if the task has been
     *                scheduled or canceled.
     */
 public void schedule(TimerTask task, Date when)
 {
     if (when.getTime() < 0) {
         throw new java.lang.IllegalArgumentException();
     }
     long delay = when.getTime() - java.lang.SystemJ.currentTimeMillis();
     scheduleImpl(task, delay < 0 ? 0 : delay, -1, false);
 }
Пример #18
0
 internal void insertTask(TimerTask newTask)
 {
     // callers are synchronized
     tasks.insert(newTask);
     this.notify();
 }