com.zman.thread.eventloop.impl.ScheduledFutureTask Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of event-loop Show documentation
Show all versions of event-loop Show documentation
a pure lightweight event-loop.
package com.zman.thread.eventloop.impl;
import java.util.concurrent.*;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
public class ScheduledFutureTask
extends FutureTask implements RunnableFuture, ScheduledFuture {
private static long SEQUENCER;
/** The time the task is enabled to execute in nanoTime units */
private long time;
/** Sequence number to break ties FIFO */
private long sequenceNumber;
/**
* Creates a one-shot action with given nanoTime-based trigger time.
*/
ScheduledFutureTask(Callable callable, long time, TimeUnit timeUnit) {
super(callable);
this.time = System.nanoTime() + timeUnit.toNanos(time);
sequenceNumber = SEQUENCER++;
}
public long getDelay(TimeUnit unit) {
return unit.convert(time - System.nanoTime(), NANOSECONDS);
}
public int compareTo(Delayed other) {
if (other == this) // compare zero if same object
return 0;
if (other instanceof ScheduledFutureTask) {
ScheduledFutureTask> x = (ScheduledFutureTask>)other;
long diff = time - x.time;
if (diff < 0)
return -1;
else if (diff > 0)
return 1;
else if (sequenceNumber < x.sequenceNumber)
return -1;
else
return 1;
}else{
throw new IllegalArgumentException("The parameter is not ScheduledFutureTask");
}
}
}