java.util.TimerTask.cancel()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(6.9k)|赞(0)|评价(0)|浏览(121)

本文整理了Java中java.util.TimerTask.cancel()方法的一些代码示例,展示了TimerTask.cancel()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TimerTask.cancel()方法的具体详情如下:
包路径:java.util.TimerTask
类名称:TimerTask
方法名:cancel

TimerTask.cancel介绍

[英]Cancels the TimerTask and removes it from the Timer's queue. Generally, it returns false if the call did not prevent a TimerTask from running at least once. Subsequent calls have no effect.
[中]取消计时器任务并将其从计时器队列中移除。通常,如果调用未阻止TimerTask至少运行一次,则返回false。后续通话无效。

代码示例

代码示例来源:origin: k9mail/k-9

private void raiseNotification() {
    if (timer != null) {
      synchronized (timer) {
        if (timerTask != null) {
          timerTask.cancel();
          timerTask = null;
        }
        timerTask = new TimerTask() {
          @Override
          public void run() {
            if (startTime != null) {
              Long endTime = SystemClock.elapsedRealtime();
              Timber.i("TracingWakeLock for tag %s / id %d: has been active for %d ms, timeout = %d ms",
                  tag, id, endTime - startTime, timeout);
            } else {
              Timber.i("TracingWakeLock for tag %s / id %d: still active, timeout = %d ms",
                  tag, id, timeout);
            }
          }
        };
        timer.schedule(timerTask, 1000, 1000);
      }
    }
  }
}

代码示例来源:origin: apollographql/apollo-android

void schedule(final int taskId, final Runnable task, long delay) {
 TimerTask timerTask = new TimerTask() {
  @Override public void run() {
   try {
    task.run();
   } finally {
    cancelTask(taskId);
   }
  }
 };
 synchronized (this) {
  TimerTask previousTimerTask = tasks.put(taskId, timerTask);
  if (previousTimerTask != null) {
   previousTimerTask.cancel();
  }
  if (timer == null) {
   timer = new Timer("Subscription SmartTimer", true);
  }
  timer.schedule(timerTask, delay);
 }
}

代码示例来源:origin: cSploit/android

@Override
public void onReceive(Context context, Intent intent) {
  synchronized (getActivity()) {
    if (mTask != null) {
      mTask.cancel();
    }
    mTask = new TimerTask() {
      @Override
      public void run() {
        check();
      }
    };
    new Timer().schedule(mTask, CHECK_DELAY);
  }
}

代码示例来源:origin: internetarchive/heritrix3

checkpointTask.cancel();
this.timer.schedule(checkpointTask, periodMs, periodMs);
LOGGER.info("Installed Checkpoint TimerTask to checkpoint every " +
    periodMs + " milliseconds.");

代码示例来源:origin: pentaho/pentaho-kettle

/**
  * Cancels current timer task if any and schedules new one to be executed immediately and then every UPDATE_TIME_VIEW
  * milliseconds.
  */
 private void engageViewAndLogUpdateTimer() {
  if ( timer == null ) {
   timer = new Timer( "SpoonSlave: " + getMeta().getName() );
  }

  if ( timerTask != null ) {
   timerTask.cancel();
  }

  timerTask = new TimerTask() {
   public void run() {
    if ( display != null && !display.isDisposed() ) {
     display.asyncExec( new Runnable() {
      public void run() {
       refreshViewAndLog();
      }
     } );
    }
   }
  };

  timer.schedule( timerTask, 0L, UPDATE_TIME_VIEW );
 }
}

代码示例来源:origin: geotools/geotools

/**
 * Set a timer for disposing the backing store after the specified amount of milliseconds of
 * inactivity. The {@link #createBackingStore} method will be responsible for creating a new
 * backing store when needed. Note that the backing store disposal can be vetoed if {@link
 * #canDisposeBackingStore} returns {@code false}.
 *
 * @param delay The minimal delay before to close the backing store. This delay is very
 *     approximative. The backing store will not be closed before, but may take as much as twice
 *     that time before to be closed.
 */
public synchronized void setTimeout(final long delay) {
  // if we have been disposed, don't do anything
  if (TIMER == null) return;
  if (disposer != null) {
    disposer.cancel();
  }
  disposer = new Disposer();
  TIMER.schedule(disposer, delay, delay);
}

代码示例来源:origin: i2p/i2p.i2p

private void scheduleTask(final RrdNioBackend rrdNioBackend) {
  final TimerTask task = new SyncTimerTask(rrdNioBackend);
  if (m_tasks.containsKey(rrdNioBackend)) {
    m_tasks.get(rrdNioBackend).cancel();
  }
  m_tasks.put(rrdNioBackend, task);
  m_timer.schedule(task, getSyncPeriod() * 1000L, getSyncPeriod() * 1000L);
}

代码示例来源:origin: stackoverflow.com

this.schedule(timerTask, delay);
timerTask.cancel();
timerTask = new TimerTask()
this.schedule(timerTask, delay);

代码示例来源:origin: org.eclipse.jetty/jetty-util

public void schedule ()
{  
  if (_running)
  {
    if (_timer!=null)
      _timer.cancel();
    if (_task!=null)
      _task.cancel();
    if (getScanInterval() > 0)
    {
      _timer = newTimer();
      _task = newTimerTask();
      _timer.schedule(_task, 1010L*getScanInterval(),1010L*getScanInterval());
    }
  }
}
/**

代码示例来源:origin: org.mortbay.jetty/jetty-util

public void schedule ()
{  
  if (_running)
  {
    if (_timer!=null)
      _timer.cancel();
    if (_task!=null)
      _task.cancel();
    if (getScanInterval() > 0)
    {
      _timer = newTimer();
      _task = newTimerTask();
      _timer.schedule(_task, 1000L*getScanInterval(),1000L*getScanInterval());
    }
  }
}
/**

代码示例来源:origin: openmrs/openmrs-core

new Timer().schedule(later, 500);
new Timer().schedule(laterStill, 10000);
laterStill.cancel();
frame.setVisible(false);
window.setVisible(false);

代码示例来源:origin: naver/ngrinder

timer.schedule(reportTimerTask, reportToConsoleInterval, reportToConsoleInterval);
    m_terminalLogger.info("This test will shut down after {} ms", duration);
    timer.schedule(shutdownTimerTask, duration);
  reportTimerTask.cancel();
  shutdownTimerTask.cancel();

代码示例来源:origin: inchaincodes/inchain

protected synchronized void resetTimeout() {
  if (timeoutTask != null)
    timeoutTask.cancel();
  if (timeoutMillis == 0 || !timeoutEnabled)
    return;
  timeoutTask = new TimerTask() {
    @Override
    public void run() {
      timeoutOccurred();
    }
  };
  timeoutTimer.schedule(timeoutTask, timeoutMillis);
}

代码示例来源:origin: apache/felix

void schedule(final TimerTask task, final long deadLine)
{
  if (deadLine < 0)
  {
    task.cancel();
  }
  else
  {
    coordinationTimer.schedule(task, new Date(deadLine));
  }
}

代码示例来源:origin: fr.acinq/bitcoinj-core

/**
 * Resets the current progress towards timeout to 0.
 */
protected synchronized void resetTimeout() {
  if (timeoutTask != null)
    timeoutTask.cancel();
  if (timeoutMillis == 0 || !timeoutEnabled)
    return;
  timeoutTask = new TimerTask() {
    @Override
    public void run() {
      timeoutOccurred();
    }
  };
  timeoutTimer.schedule(timeoutTask, timeoutMillis);
}

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

/**
 * Resets the current progress towards timeout to 0.
 */
protected synchronized void resetTimeout() {
  if (timeoutTask != null)
    timeoutTask.cancel();
  if (timeoutMillis == 0 || !timeoutEnabled)
    return;
  timeoutTask = new TimerTask() {
    @Override
    public void run() {
      timeoutOccurred();
    }
  };
  timeoutTimer.schedule(timeoutTask, timeoutMillis);
}

代码示例来源:origin: com.google/bitcoinj

/**
 * Resets the current progress towards timeout to 0.
 */
protected synchronized void resetTimeout() {
  if (timeoutTask != null)
    timeoutTask.cancel();
  if (timeoutMillis == 0 || !timeoutEnabled)
    return;
  timeoutTask = new TimerTask() {
    @Override
    public void run() {
      timeoutOccurred();
    }
  };
  timeoutTimer.schedule(timeoutTask, timeoutMillis);
}

代码示例来源:origin: greenaddress/GreenBits

/**
 * Resets the current progress towards timeout to 0.
 */
protected synchronized void resetTimeout() {
  if (timeoutTask != null)
    timeoutTask.cancel();
  if (timeoutMillis == 0 || !timeoutEnabled)
    return;
  timeoutTask = new TimerTask() {
    @Override
    public void run() {
      timeoutOccurred();
    }
  };
  timeoutTimer.schedule(timeoutTask, timeoutMillis);
}

代码示例来源:origin: org.infobip.mobile.messaging.api/infobip-mobile-messaging-android-sdk

public synchronized void put(final Runnable task) {
    if (this.timerTask != null) {
      timerTask.cancel();
      timer.purge();
    }
    timerTask = new TimerTask() {
      @Override
      public void run() {
        task.run();
      }
    };
    timer.schedule(timerTask, delay);
  }
}

代码示例来源:origin: com.threerings.nexus/nexus-server

@Override public Nexus.Deferred repeatEvery (long period) {
  Preconditions.checkState(task != null, "Deferred action has been canceled.");
  task.cancel();
  task = createTask();
  _timer.schedule(task, delay, period);
  return this;
}
private TimerTask createTask () {

相关文章