下面列出了java.util.concurrent.ScheduledFuture#isCancelled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void disposeHelper() {
String helperKey = buildHelperKey(ontologySettings);
if (checkers.containsKey(helperKey)) {
ScheduledFuture checker = checkers.remove(helperKey);
if (!checker.isCancelled() && !checker.isDone()) {
logger.debug("Cancelling ScheduledFuture for {}", helperKey);
checker.cancel(false);
}
}
if (helpers.containsKey(helperKey)) {
OntologyHelper helper = helpers.remove(helperKey);
helper.dispose();
}
threadPool.stats();
logger.debug("helpers.size() = {}; checkers.size() = {}", helpers.size(), checkers.size());
}
@Override
protected void stopBackgroundDiscovery() {
logger.debug("Stopping LIFX device background discovery");
ScheduledFuture<?> localDiscoveryJob = discoveryJob;
if (localDiscoveryJob != null && !localDiscoveryJob.isCancelled()) {
localDiscoveryJob.cancel(true);
discoveryJob = null;
}
ScheduledFuture<?> localNetworkJob = networkJob;
if (localNetworkJob != null && !localNetworkJob.isCancelled()) {
localNetworkJob.cancel(true);
networkJob = null;
}
}
public void start() {
try {
lock.lock();
logger.debug("{} : Starting communication handler", logId);
logger.debug("{} : Using '{}' as source identifier", logId, Long.toString(sourceId, 16));
ScheduledFuture<?> localNetworkJob = networkJob;
if (localNetworkJob == null || localNetworkJob.isCancelled()) {
networkJob = scheduler.scheduleWithFixedDelay(this::receiveAndHandlePackets, 0, PACKET_INTERVAL,
TimeUnit.MILLISECONDS);
}
currentLightState.setOffline();
Selector localSelector = Selector.open();
selector = localSelector;
if (isBroadcastEnabled()) {
broadcastKey = openBroadcastChannel(selector, logId, broadcastPort);
selectorContext = new LifxSelectorContext(localSelector, sourceId, sequenceNumberSupplier, logId, host,
macAddress, broadcastKey, unicastKey);
broadcastPacket(new GetServiceRequest());
} else {
unicastKey = openUnicastChannel(selector, logId, host);
selectorContext = new LifxSelectorContext(localSelector, sourceId, sequenceNumberSupplier, logId, host,
macAddress, broadcastKey, unicastKey);
sendPacket(new GetServiceRequest());
}
} catch (IOException e) {
logger.error("{} while starting LIFX communication handler for light '{}' : {}",
e.getClass().getSimpleName(), logId, e.getMessage(), e);
} finally {
lock.unlock();
}
}
/**
* Happy path with single task to be executed periodically until it's canceled.
*
* @throws InterruptedException we don't expect it
*/
@Test
public void periodicFixedDelayTask() throws InterruptedException {
final CountDownLatch countDownLatch = new CountDownLatch(4); // execute 4 times
final ScheduledFuture<?> scheduledFuture = scheduledService.periodicFixedDelayTask(1, null, countDownLatch);
LOGGER.info("Do some other work while we wait for the tasks");
countDownLatch.await(500, TimeUnit.MILLISECONDS);
if (!scheduledFuture.isCancelled()) {
scheduledFuture.cancel(true);
LOGGER.info("task stopped");
}
}
public void close() {
System.out.println("TaskCenter closing");
this.monitoring.cancel(true);
// 停止所有定时任务
for (ScheduledFuture<?> sf : this.scheduledFutureList) {
if (!sf.isCancelled() || !sf.isDone()) {
sf.cancel(true);
}
}
this.scheduledFutureList.clear();
Iterator<Timer> iter = this.timers.values().iterator();
while (iter.hasNext()) {
Timer timer = iter.next();
timer.cancel();
}
this.timers.clear();
// 关闭滑动窗
this.slidingWindow.stop();
// 关闭线程池
this.mainExecutor.shutdown();
this.scheduledExecutor.shutdown();
System.out.println("TaskCenter closed");
}
public void stop() {
try {
lock.lock();
communicationHandler.removeResponsePacketListener(this::handleResponsePacket);
ScheduledFuture<?> localUpdateJob = updateJob;
if (localUpdateJob != null && !localUpdateJob.isCancelled()) {
localUpdateJob.cancel(true);
updateJob = null;
}
} catch (Exception e) {
logger.error("Error occurred while stopping properties update job for a light ({})", logId, e);
} finally {
lock.unlock();
}
}
private void stopHeartbeatTimer() {
try {
ScheduledFuture<?> timer = heatbeatTimer;
if (timer != null && ! timer.isCancelled()) {
timer.cancel(true);
}
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
} finally {
heatbeatTimer =null;
}
}
private void stopHeartbeatTimer() {
try {
ScheduledFuture<?> timer = heatbeatTimer;
if (timer != null && ! timer.isCancelled()) {
timer.cancel(true);
}
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
} finally {
heatbeatTimer =null;
}
}
@Nonnull
public SimpleTimerTask setUp(@Nonnull final Runnable runnable, final long delay) {
final ScheduledFuture<?> future = myScheduledExecutorService.schedule(runnable, delay, TimeUnit.MILLISECONDS);
return new SimpleTimerTask() {
@Override
public void cancel() {
future.cancel(false);
}
@Override
public boolean isCancelled() {
return future.isCancelled();
}
};
}
/**
* Stops repeatedly scanning for newly added and removed USB devices. This can be restarted using
* {@link #startBackgroundScanning()}.
*/
@Override
public synchronized void stopBackgroundScanning() {
logger.debug("Stopping USB-Serial background discovery");
ScheduledFuture<?> currentBackgroundScanningJob = backgroundScanningJob;
if (currentBackgroundScanningJob != null && !currentBackgroundScanningJob.isCancelled()) {
if (currentBackgroundScanningJob.cancel(true)) {
backgroundScanningJob = null;
logger.debug("Stopped USB-serial background discovery");
}
}
}
public void start() {
try {
lock.lock();
communicationHandler.addResponsePacketListener(this::handleResponsePacket);
ScheduledFuture<?> localStatePollingJob = statePollingJob;
if (localStatePollingJob == null || localStatePollingJob.isCancelled()) {
statePollingJob = scheduler.scheduleWithFixedDelay(this::pollLightState, 0, STATE_POLLING_INTERVAL,
TimeUnit.SECONDS);
}
} catch (Exception e) {
logger.error("Error occurred while starting light state updater", e);
} finally {
lock.unlock();
}
}
private void stopHeartbeatTimer() {
try {
ScheduledFuture<?> timer = heatbeatTimer;
if (timer != null && !timer.isCancelled()) {
timer.cancel(true);
}
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
} finally {
heatbeatTimer = null;
}
}
private void stopHeartbeatTimer() {
try {
ScheduledFuture<?> timer = heatbeatTimer;
if (timer != null && ! timer.isCancelled()) {
timer.cancel(true);
}
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
} finally {
heatbeatTimer =null;
}
}
public void start() {
try {
lock.lock();
communicationHandler.addResponsePacketListener(this::handleResponsePacket);
ScheduledFuture<?> localEchoJob = echoJob;
if (localEchoJob == null || localEchoJob.isCancelled()) {
echoJob = scheduler.scheduleWithFixedDelay(this::sendEchoPackets, 0, ECHO_POLLING_INTERVAL,
TimeUnit.SECONDS);
}
} catch (Exception e) {
logger.error("Error occurred while starting online state poller for a light ({})", logId, e);
} finally {
lock.unlock();
}
}
@Override
protected void startBackgroundDiscovery() {
logger.debug("Starting the LIFX device background discovery");
ScheduledFuture<?> localDiscoveryJob = discoveryJob;
if (localDiscoveryJob == null || localDiscoveryJob.isCancelled()) {
discoveryJob = scheduler.scheduleWithFixedDelay(this::doScan, 0, REFRESH_INTERVAL, TimeUnit.SECONDS);
}
}
private void stopHeartbeatTimer() {
try {
ScheduledFuture<?> timer = heatbeatTimer;
if (timer != null && ! timer.isCancelled()) {
timer.cancel(true);
}
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
} finally {
heatbeatTimer =null;
}
}
/**
* Stops repeatedly scanning for newly added and removed USB devices. This can be restarted using
* {@link #startBackgroundScanning()}.
*/
@Override
public synchronized void stopBackgroundScanning() {
logger.debug("Stopping USB-Serial background discovery");
ScheduledFuture<?> currentBackgroundScanningJob = backgroundScanningJob;
if (currentBackgroundScanningJob != null && !currentBackgroundScanningJob.isCancelled()) {
if (currentBackgroundScanningJob.cancel(true)) {
backgroundScanningJob = null;
logger.debug("Stopped USB-serial background discovery");
}
}
}
public boolean isCancelled() {
ScheduledFuture<?> future = this.future;
if (future == null) return true;
return future.isCancelled();
}
private void cancelRingingTimeout() {
final ScheduledFuture<?> future = this.ringingTimeoutFuture;
if (future != null && !future.isCancelled()) {
future.cancel(false);
}
}
public void stopSchedule(Long id) throws ScheduleNotRunningException {
if (runningSchedules.containsKey(id)) {
LOGGER.info("Stopping schedule {}", id);
ScheduledFuture<?> future = runningSchedules.get(id).getFuture();
if (!future.isCancelled()) {
future.cancel(true);
runningSchedules.remove(id);
LOGGER.info("Schedule should now be stopped");
}
} else {
throw new ScheduleNotRunningException();
}
}