Example usage for java.util.concurrent ScheduledFuture getDelay

List of usage examples for java.util.concurrent ScheduledFuture getDelay

Introduction

In this page you can find the example usage for java.util.concurrent ScheduledFuture getDelay.

Prototype

long getDelay(TimeUnit unit);

Source Link

Document

Returns the remaining delay associated with this object, in the given time unit.

Usage

From source file:com.bt.aloha.util.CollectionHelper.java

public static void destroy(ConcurrentMap<String, Map<String, Object>> transients, String classname) {
    log.debug(String.format("Destroy method called on collection: %s", classname));
    for (Map<String, Object> element : transients.values()) {
        ScheduledFuture<?> future = (ScheduledFuture<?>) element.get("future");
        if (future == null || future.isCancelled() || future.isDone())
            continue;
        if (future.getDelay(TimeUnit.MILLISECONDS) > ONE_HUNDRED) {
            future.cancel(true);/*from ww  w .ja v  a2  s.c o  m*/
            continue;
        }
        int counter = 0;
        while (!future.isDone() && counter++ < THREE) {
            try {
                log.debug("Waiting for future to get done for some call...");
                Thread.sleep(ONE_THOUSAND);
            } catch (InterruptedException e) {
                log.warn(e.getMessage());
                continue;
            }
        }
    }
}

From source file:com.mgmtp.perfload.core.client.util.concurrent.DelayingExecutorServiceTest.java

@Test
public void testWithDelay() throws InterruptedException, BrokenBarrierException {
    DelayingExecutorService execSrv = new DelayingExecutorService();

    final StopWatch sw = new StopWatch();

    final CyclicBarrier stopBarrier = new CyclicBarrier(11, new Runnable() {
        @Override/*from   ww w .  j a va 2  s  .co m*/
        public void run() {
            sw.stop();
        }
    });

    sw.start();

    final long taskSleepMillis = 75L;
    long delayMultiplier = 50L;
    int loopMax = 10;

    for (int i = 0; i < loopMax; ++i) {
        Callable<Void> c = new Callable<Void>() {
            @Override
            public Void call() {
                try {
                    Thread.sleep(taskSleepMillis);
                    stopBarrier.await();
                } catch (Exception ex) {
                    throw new AssertionError(ex);
                }
                return null;
            }
        };

        long delay = delayMultiplier * i;

        ScheduledFuture<?> future = execSrv.schedule(c, delay, TimeUnit.MILLISECONDS);
        long actualDelay = future.getDelay(TimeUnit.MILLISECONDS);

        // compare with epsilon to make up for bad accuracy
        assertTrue(abs(delay - actualDelay) < EPSILON);
    }

    stopBarrier.await();

    long actualTime = sw.getTime();
    long expectedTime = delayMultiplier * (loopMax - 1) + taskSleepMillis;

    // compare with epsilon to make up for bad accuracy
    assertTrue(abs(actualTime - expectedTime) < EPSILON);
}

From source file:com.mgmtp.perfload.core.client.util.concurrent.DelayingExecutorServiceTest.java

@Test
public void testWithoutDelay() throws InterruptedException, BrokenBarrierException {
    DelayingExecutorService execSrv = new DelayingExecutorService();

    final StopWatch sw = new StopWatch();

    final CyclicBarrier stopBarrier = new CyclicBarrier(11, new Runnable() {
        @Override/*w w w  .j  a v  a2 s . c  o  m*/
        public void run() {
            sw.stop();
        }
    });

    sw.start();

    for (int i = 0; i < 10; ++i) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1L);
                    stopBarrier.await();
                } catch (Exception ex) {
                    throw new AssertionError(ex);
                }
            }
        };

        ScheduledFuture<?> future = execSrv.schedule(r, 0L, TimeUnit.NANOSECONDS);

        // compare with epsilon to make up for bad accuracy
        assertTrue(abs(future.getDelay(TimeUnit.MILLISECONDS)) < EPSILON);
    }

    stopBarrier.await();

    assertTrue(sw.getTime() < EPSILON);
}

From source file:com.l2jfree.gameserver.gameobjects.L2Creature.java

/**
 * Disable this skill id for the duration of the delay in milliseconds.
 * //from w  ww  . j  a v  a2  s .c o m
 * @param skillId
 * @param delay in milliseconds
 * @return modified
 */
public boolean disableSkill(int skillId, int delay) {
    if (delay < 100)
        return false;

    if (_disabledSkills == null)
        _disabledSkills = new FastMap<Integer, ScheduledFuture<?>>();

    final ScheduledFuture<?> oldTask = _disabledSkills.get(skillId);

    if (oldTask != null) {
        if (oldTask.getDelay(TimeUnit.MILLISECONDS) + 50 >= delay)
            return false;

        oldTask.cancel(false);
    }

    _disabledSkills.put(skillId, ThreadPoolManager.getInstance().schedule(new EnableSkill(skillId), delay));
    return true;
}