下面列出了java.util.concurrent.ScheduledFuture#getDelay ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void waitForTermination(
Logger log, Future<?> future, long waitTimeInMillis, String msg, Object... msgParams) {
if (future == null) {
return;
}
boolean completedOrCancelled = future.isCancelled() || future.isDone();
if (!completedOrCancelled) {
if (future instanceof ScheduledFuture) {
ScheduledFuture f = (ScheduledFuture) future;
if (f.getDelay(TimeUnit.MILLISECONDS) > Constants.MIN_DELAY) {
return;
}
}
}
waitForShutdown(log, future, waitTimeInMillis, msg, msgParams);
}
/**
* For JMX to forget about all previously exported metrics.
*/
public static void clearJmxCache() {
if (LOG.isTraceEnabled()) {
LOG.trace("clearing JMX Cache" + StringUtils.stringifyException(new Exception()));
}
//If there are more then 100 ms before the executor will run then everything should be merged.
ScheduledFuture future = fut.get();
if ((future != null && (!future.isDone() && future.getDelay(TimeUnit.MILLISECONDS) > 100))) {
// BAIL OUT
return;
}
if (stopped.get()) {
return;
}
future = executor.getExecutor().schedule(new JmxCacheBusterRunnable(), 5, TimeUnit.SECONDS);
fut.set(future);
}
@Test
public void scheduleCallableCancelTest() {
ScheduledExecutorService scheduler = makeScheduler(1);
try {
TestCallable tcDelay = new TestCallable(0);
ScheduledFuture<Object> delayF = scheduler.schedule(tcDelay, 20, TimeUnit.MILLISECONDS);
long delay = delayF.getDelay(TimeUnit.MILLISECONDS);
boolean canceled = delayF.cancel(true);
assertTrue(delay <= 20);
if (canceled) {
assertTrue(delayF.isCancelled());
}
} finally {
scheduler.shutdownNow();
}
}
@Override
public long getDelay(TimeUnit unit) {
ScheduledFuture<?> curr;
synchronized (this.triggerContextMonitor) {
curr = obtainCurrentFuture();
}
return curr.getDelay(unit);
}
@Override
public ZonedDateTime getScheduledTime(String id) {
if (scheduledJobs.containsKey(id)) {
ScheduledFuture<?> scheduled = scheduledJobs.get(id);
long remainingTime = scheduled.getDelay(TimeUnit.MILLISECONDS);
if (remainingTime > 0) {
return ZonedDateTime.from(Instant.ofEpochMilli(System.currentTimeMillis() + remainingTime));
}
}
return null;
}
@Override
public long getDelay(TimeUnit unit) {
ScheduledFuture<?> curr;
synchronized (this.triggerContextMonitor) {
curr = obtainCurrentFuture();
}
return curr.getDelay(unit);
}
@Override
public long getDelay(TimeUnit unit) {
ScheduledFuture<?> curr;
synchronized (this.triggerContextMonitor) {
curr = this.currentFuture;
}
return curr.getDelay(unit);
}
@Override
public long getDelay(TimeUnit unit) {
ScheduledFuture<?> curr;
synchronized (this.triggerContextMonitor) {
curr = this.currentFuture;
}
return curr.getDelay(unit);
}
/**
* {@inheritDoc}
*
* @see java.util.concurrent.Delayed#getDelay(java.util.concurrent.TimeUnit)
*
*/
@Override
public long getDelay(TimeUnit unit) {
ScheduledFuture<?> curr;
synchronized (this.triggerContextMonitor) {
curr = this.currentFuture;
}
return curr.getDelay(unit);
}
private static void test1() throws InterruptedException {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime());
int delay = 3;
ScheduledFuture<?> future = executor.schedule(task, delay, TimeUnit.SECONDS);
TimeUnit.MILLISECONDS.sleep(1337);
long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS);
System.out.printf("Remaining Delay: %sms\n", remainingDelay);
}
/**
* When player clicks on dead NPC to request drop list
*
* @param player
* @param npcId
*/
public void requestDropList(Player player, int npcId) {
DropNpc dropNpc = DropRegistrationService.getInstance().getDropRegistrationMap().get(npcId);
if (player == null || dropNpc == null) {
return;
}
if (!dropNpc.containsKey(player.getObjectId()) && !dropNpc.isFreeForAll()) {
PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_LOOT_NO_RIGHT);
return;
}
if (dropNpc.isBeingLooted()) {
PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_LOOT_FAIL_ONLOOTING);
return;
}
Set<DropItem> dropItems = DropRegistrationService.getInstance().getCurrentDropMap().get(npcId);
if (dropItems == null || dropItems.size() == 0) {
dropItems = Collections.emptySet();
ThreadPoolManager.getInstance().schedule(new Runnable() {
@Override
public void run() {
return;
}
}, 350); //Blocks Loot for 350ms and return if DropList is empty
}
else {
dropNpc.setBeingLooted(player);
VisibleObject visObj = World.getInstance().findVisibleObject(npcId);
if (visObj instanceof Npc) {
Npc npc = ((Npc) visObj);
ScheduledFuture<?> decayTask = (ScheduledFuture<?>) npc.getController().cancelTask(TaskId.DECAY);
if (decayTask != null) {
long reamingDecayTime = decayTask.getDelay(TimeUnit.MILLISECONDS);
dropNpc.setReamingDecayTime(reamingDecayTime);
}
}
PacketSendUtility.sendPacket(player, new SM_LOOT_ITEMLIST(npcId, dropItems, player));
PacketSendUtility.sendPacket(player, new SM_LOOT_STATUS(npcId, 2));
player.unsetState(CreatureState.ACTIVE);
player.setState(CreatureState.LOOTING);
player.setLootingNpcOid(npcId);
PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, EmotionType.START_LOOT, 0, npcId), true);
}
}
private static void test1() throws InterruptedException {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime() + ", Now = " + Instant.now());
int delay = 3;
ScheduledFuture<?> future = executor.schedule(task, delay, TimeUnit.SECONDS);
TimeUnit.MILLISECONDS.sleep(1337);
long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS);
System.out.printf("Remaining Delay: %sms\n", remainingDelay);
TimeUnit.MILLISECONDS.sleep(10000);
shutDown(executor);
}