/** Removes the task. */ public void remove() { startTask.cancel(); endTask.cancel(); calendarService.removeFromTaskMap(id); calendarService.removeFromCurrentItems(this); calendarService.updateStateFromCurrentItems(); }
public synchronized boolean pause() { if (timer == null) { return false; } stateSync.lock(); try { shouldRun = false; if (null != task) { task.cancel(); task = null; } animThread = null; try { final long periodx2 = 2L * (long) (1000.0f / (float) fps); Thread.sleep( periodx2 > 20L ? periodx2 : 20L); // max(2 x timer period, ~ 1/60), since we can't ctrl stopped threads } catch (InterruptedException e) { } } finally { stateSync.unlock(); } return true; }
private void resetClientTimer(Socket socket) { if (this.idleClientTimer == null) { if (log.logTrace()) { log.trace("No idle client timeout configured, skipping timer reset"); } return; } if (log.logTrace()) { log.trace("Resetting idle client timer: " + System.identityHashCode(socket)); } // Store this in a local so we don't have to worry about // the value changing underneath us. long timeout = this.idleClientTimeout; // Cancel the existing task for this socket ... TimerTask curTask = (TimerTask) this.socketActivity.get(socket); if (curTask != null) { curTask.cancel(); } // And schedule a new one. TimerTask task = new IdleClientTimerTask(socket); this.socketActivity.put(socket, task); this.idleClientTimer.schedule(task, timeout); }
/** 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) { } } }
protected void deregisterSocket(Socket socket) { TimerTask task = (TimerTask) this.socketActivity.remove(socket); if (task != null) { task.cancel(); } super.deregisterSocket(socket); }
@Override public void onPause() { Log.d(TAG, "LoaderActivity paused"); super.onPause(); mTimerTask.cancel(); mTimerTask = null; }
public void messageReceived(int to, Message m) { DripMsg msg = (DripMsg) m; int newId = msg.get_metadata_id(); int newSeqno = msg.get_metadata_seqno(); log.debug("Received Msg: id=" + newId + ",seqno=" + newSeqno); if (newId != id) { log.debug("dropped, not ID " + id); return; } if ((newSeqno & ~DripConsts.DRIP_WAKEUP_BIT) == DripConsts.DRIP_SEQNO_NEWEST) { log.debug("dropped, a special seqno " + newSeqno); return; } switch (state) { case PROBING: seqno = newSeqno; log.info( "Receive: id=" + id + ",seqno=" + dripMsg.get_metadata_seqno() + " Heard Old Seqno"); incrementSeqno(); state = SENDING_SEQNO; case SENDING_SEQNO: if (seqno == newSeqno) { log.info( "Receive: id=" + id + ",seqno=" + dripMsg.get_metadata_seqno() + " Heard New Seqno"); trickle.cancel(); trickleTask.cancel(); sendDone(); } default: } }
public SimpleTimerTask setUp(final Runnable runnable, long delay) { synchronized (myTime2Task) { final long current = System.currentTimeMillis(); final long targetTime = current + delay; final SimpleTimerTask result = new SimpleTimerTask(targetTime, runnable, this); ArrayList<SimpleTimerTask> tasks = myTime2Task.get(targetTime); if (tasks == null) { tasks = new ArrayList<SimpleTimerTask>(2); myTime2Task.put(targetTime, tasks); } tasks.add(result); if (targetTime < myNextScheduledTime) { if (myNextProcessingTask != null) { myNextProcessingTask.cancel(); } scheduleNext(delay, targetTime); } return result; } }
/** Cancel any running Timer. */ private void cancelTimer() { if (task != null) { task.cancel(); task = null; } }
/** Stops the cleaning thread. */ public void finalize() { clean.cancel(); }