protected Task nextTask() {

        try {
            while (!this.isShutdown || Thread.interrupted()) {
                long now;
                long next;
                long wait;
                Task task;

                // synchronized to ensure removeTask
                // applies only to the peeked task,
                // when the computed wait <= 0
                synchronized (this.taskQueue) {
                    task = this.taskQueue.peekTask();

                    if (task == null) {

                        // queue is empty
                        break;
                    }

                    now  = System.currentTimeMillis();
                    next = task.next;
                    wait = (next - now);

                    if (wait > 0) {

                        // release ownership of taskQueue monitor and await
                        // notification of task addition or cancellation,
                        // at most until the time when the peeked task is
                        // next supposed to execute
                        this.taskQueue.park(wait);

                        continue;           // to top of loop
                    } else {
                        this.taskQueue.removeTask();
                    }
                }

                long period = task.period;

                if (period > 0) {           // repeated task
                    if (task.relative) {    // using fixed rate shceduling
                        final long late = (now - next);

                        if (late > period) {

                            // ensure that really late tasks don't
                            // completely saturate the head of the
                            // task queue
                            period = 0;     /** @todo : is -1, -2 ... fairer? */
                        } else if (late > 0) {

                            // compensate for scheduling overruns
                            period -= late;
                        }
                    }

                    task.updateSchedule(now, now + period);
                    this.taskQueue.addTask(task);
                }

                return task;
            }
        } catch (InterruptedException e) {

            //e.printStackTrace();
        }

        return null;
    }
protected Task nextTask ( ) { try { while ( ! this . isShutdown || Thread . interrupted ( ) ) { long now ; long next ; long wait ; Task task ; synchronized ( this . taskQueue ) { task = this . taskQueue . peekTask ( ) ; if ( task == null ) { break ; } now = System . currentTimeMillis ( ) ; next = task . next ; wait = ( next - now ) ; if ( wait > 0 ) { this . taskQueue . park ( wait ) ; continue ; } else { this . taskQueue . removeTask ( ) ; } } long period = task . period ; if ( period > 0 ) { if ( task . relative ) { final long late = ( now - next ) ; if ( late > period ) { period = 0 ; } else if ( late > 0 ) { period -= late ; } } task . updateSchedule ( now , now + period ) ; this . taskQueue . addTask ( task ) ; } return task ; } } catch ( InterruptedException e ) { } return null ; }
Retrieves the next task to execute, or null if this timer is shutdown,
the current thread is interrupted, or there are no queued tasks.

@return the next task to execute, or null