/** The main timer loop. (See class comment.) */ private void mainLoop() { while (true) { try { TimerTask task; boolean taskFired; synchronized (queue) { // Wait for queue to become non-empty // But no more than timeout value. while (queue.isEmpty() && queue.newTasksMayBeScheduled) { queue.wait(THREAD_TIMEOUT); if (queue.isEmpty()) { break; } } if (queue.isEmpty()) break; // Queue is empty and will forever remain; die // Handle a possible change of the user clock queue.checkUserClockChange(); // Queue nonempty; look at first evt and do the right thing long currentTime, executionTime; task = queue.getMin(); synchronized (task.lock) { if (task.state == TimerTask.CANCELLED) { queue.removeMin(); continue; // No action required, poll queue again } currentTime = Timer.monotonicTimeMillis(); executionTime = task.nextExecutionTime; if (taskFired = (executionTime <= currentTime)) { if (task.period == 0) { // Non-repeating, remove queue.removeMin(); task.state = TimerTask.EXECUTED; } else { // Repeating task, reschedule queue.rescheduleMin( task.period < 0 ? currentTime - task.period : executionTime + task.period); } } } if (!taskFired) { // Task hasn't yet fired; wait long timeout = executionTime - currentTime; if (queue.hasUserClockTasks() && timeout > Timer.USER_CLOCK_CHECK_PERIOD) { timeout = Timer.USER_CLOCK_CHECK_PERIOD; } queue.wait(timeout); } } if (taskFired) { // Task fired; run it, holding no locks try { task.run(); } catch (Exception e) { // Cancel tasks that cause exceptions task.cancel(); } } } catch (InterruptedException e) { } } }
/** Schedules the start and end tasks of the calendar item. */ public void scheduleTasks() { if (!executeNow) { timer.schedule(startTask, startDate); } else { startTask.run(); } timer.schedule(endTask, endDate); }
public void expire() { if (updateState(ST_INIT, ST_EXPIRED)) { try { task.run(this); } catch (Throwable t) { parent.logger.warning( "An exception was thrown by " + TimerTask.class.getSimpleName() + ".", t); } } }
public void expire() { if (!state.compareAndSet(ST_INIT, ST_EXPIRED)) { return; } try { task.run(this); } catch (Throwable t) { if (logger.isWarnEnabled()) { logger.warn("An exception was thrown by " + TimerTask.class.getSimpleName() + '.', t); } } }
/** * Executes example. * * @param args Command line arguments, none required. * @throws GridException If example execution failed. */ public static void main(String[] args) throws Exception { Timer timer = new Timer("priceBars"); // Start grid. final Grid g = GridGain.start("examples/config/example-streamer.xml"); System.out.println(); System.out.println(">>> Streaming price bars example started."); try { TimerTask task = scheduleQuery(g, timer); streamData(g); // Force one more run to get final results. task.run(); timer.cancel(); // Reset all streamers on all nodes to make sure that // consecutive executions start from scratch. g.compute() .broadcast( new Runnable() { @Override public void run() { if (!ExamplesUtils.hasStreamer(g, "priceBars")) System.err.println( "Default streamer not found (is example-streamer.xml " + "configuration used on all nodes?)"); else { GridStreamer streamer = g.streamer("priceBars"); System.out.println("Clearing bars from streamer."); streamer.reset(); } } }) .get(); } finally { GridGain.stop(true); } }
/** This method will be launched on separate thread for each Timer object. */ @Override public void run() { while (true) { final TimerTask task; synchronized (this) { // need to check cancelled inside the synchronized block if (cancelled) { return; } if (tasks.isEmpty()) { if (finished) { return; } // no tasks scheduled -- sleep until any task appear try { this.wait(); } catch (InterruptedException ignored) { } continue; } long currentTime = System.currentTimeMillis(); task = tasks.minimum(); long timeToSleep; synchronized (task.lock) { if (task.cancelled) { tasks.delete(0); continue; } // check the time to sleep for the first task scheduled timeToSleep = task.when - currentTime; } if (timeToSleep > 0) { // sleep! try { this.wait(timeToSleep); } catch (InterruptedException ignored) { } continue; } // no sleep is necessary before launching the task synchronized (task.lock) { int pos = 0; if (tasks.minimum().when != task.when) { pos = tasks.getTask(task); } if (task.cancelled) { tasks.delete(tasks.getTask(task)); continue; } // set time to schedule task.setScheduledTime(task.when); // remove task from queue tasks.delete(pos); // set when the next task should be launched if (task.period >= 0) { // this is a repeating task, if (task.fixedRate) { // task is scheduled at fixed rate task.when = task.when + task.period; } else { // task is scheduled at fixed delay task.when = System.currentTimeMillis() + task.period; } // insert this task into queue insertTask(task); } else { task.when = 0; } } } boolean taskCompletedNormally = false; try { task.run(); taskCompletedNormally = true; } finally { if (!taskCompletedNormally) { synchronized (this) { cancelled = true; } } } } }