下面列出了怎么用java.util.concurrent.Delayed的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public int compareTo(Delayed other) {
if (this == other) {
return 0;
}
if (other instanceof GridmixJob) {
final long otherNanos = ((GridmixJob)other).submissionTimeNanos;
if (otherNanos < submissionTimeNanos) {
return 1;
}
if (otherNanos > submissionTimeNanos) {
return -1;
}
return id() - ((GridmixJob)other).id();
}
final long diff =
getDelay(TimeUnit.NANOSECONDS) - other.getDelay(TimeUnit.NANOSECONDS);
return 0 == diff ? 0 : (diff > 0 ? 1 : -1);
}
/**
* 元素的先后顺序
* @param other
* @return
*/
@Override
public int compareTo(Delayed other) {
if (other == this) // compare zero ONLY if same object
return 0;
if (other instanceof Task) {
Task x = (Task) other;
long diff = timeOut - x.timeOut;
if (diff < 0)
return -1;
else if (diff > 0)
return 1;
else if (sequenceNum < x.sequenceNum)
return -1;
else
return 1;
}
long d = (getDelay(TimeUnit.MILLISECONDS) - other.getDelay(TimeUnit.MILLISECONDS));
return (d == 0) ? 0 : ((d < 0) ? -1 : 1);
}
@Override
public int compareTo(Delayed o) {
if (this == o) {
return 0;
}
ScheduledFutureTask<?> that = (ScheduledFutureTask<?>) o;
long d = deadlineNanos() - that.deadlineNanos();
if (d < 0) {
return -1;
} else if (d > 0) {
return 1;
} else if (id < that.id) {
return -1;
} else if (id == that.id) {
throw new Error();
} else {
return 1;
}
}
@Override
public int compareTo(Delayed other) {
if (other instanceof ThrottlingDelayQueue.TenantThrottler) {
long onext = ((ThrottlingDelayQueue.TenantThrottler) other).next;
if (next < onext)
return -1;
else if (next == onext)
return 0;
else
return 1;
} else {
long odelay = other.getDelay(TimeUnit.NANOSECONDS);
long tdelay = this.getDelay(TimeUnit.NANOSECONDS);
if (tdelay < odelay)
return -1;
else if (tdelay == odelay)
return 0;
else
return 1;
}
}
@Override
public int compareTo(Delayed o) {
if (this == o) {
return 0;
} else if (o instanceof DelayedTaskWrapper) {
return (int)(task.getRunTime() - ((DelayedTaskWrapper)o).task.getRunTime());
} else {
long thisDelay = this.getDelay(TimeUnit.MILLISECONDS);
long otherDelay = o.getDelay(TimeUnit.MILLISECONDS);
if (thisDelay == otherDelay) {
return 0;
} else if (thisDelay > otherDelay) {
return 1;
} else {
return -1;
}
}
}
@Override
public int compareTo(Delayed other) {
if (this == other) {
return 0;
}
if (other instanceof GridmixJob) {
final long otherNanos = ((GridmixJob)other).submissionTimeNanos;
if (otherNanos < submissionTimeNanos) {
return 1;
}
if (otherNanos > submissionTimeNanos) {
return -1;
}
return id() - ((GridmixJob)other).id();
}
final long diff =
getDelay(TimeUnit.NANOSECONDS) - other.getDelay(TimeUnit.NANOSECONDS);
return 0 == diff ? 0 : (diff > 0 ? 1 : -1);
}
@Override
public int compareTo(Delayed o)
{
if ( o == this )
{
return 0;
}
long diff = getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS);
if ( diff == 0 )
{
if ( o instanceof OperationAndData )
{
diff = ordinal.get() - ((OperationAndData)o).ordinal.get();
}
}
return (diff < 0) ? -1 : ((diff > 0) ? 1 : 0);
}
@Override
public int compareTo(Delayed that) {
long thisDelay = this.getDelay(TimeUnit.MILLISECONDS);
long thatDelay = that.getDelay(TimeUnit.MILLISECONDS);
if (thisDelay < thatDelay) {
return -1;
}
if (thisDelay > thatDelay) {
return 1;
}
// 时间判断无法区分时,执行如下判断(用于维持 compareTo 的使用约束)
if (this.equals(that)) {
return 0;
} else {
return this.hashCode() - that.hashCode();
}
}
@Override
public int compareTo(Delayed that) {
long thisDelay = this.getDelay(TimeUnit.MILLISECONDS);
long thatDelay = that.getDelay(TimeUnit.MILLISECONDS);
if (thisDelay < thatDelay) {
return -1;
}
if (thisDelay > thatDelay) {
return 1;
}
// 时间判断无法区分时,执行如下判断(用于维持 compareTo 的使用约束)
if (this.equals(that)) {
return 0;
} else {
return this.hashCode() - that.hashCode();
}
}
@Override
public int compareTo(Delayed that) {
long thisDelay = this.getDelay(TimeUnit.MILLISECONDS);
long thatDelay = that.getDelay(TimeUnit.MILLISECONDS);
if (thisDelay < thatDelay) {
return -1;
}
if (thisDelay > thatDelay) {
return 1;
}
// 时间判断无法区分时,执行如下判断(用于维持 compareTo 的使用约束)
if (this.equals(that)) {
return 0;
} else {
return this.hashCode() - that.hashCode();
}
}
@Override
public int compareTo(Delayed o) {
if (this == o) {
return 0;
} else if (o instanceof TestDelayed) {
return (int)(delayInMs - ((TestDelayed)o).delayInMs);
} else {
long thisDelay = this.getDelay(TimeUnit.MILLISECONDS);
long otherDelay = o.getDelay(TimeUnit.MILLISECONDS);
if (thisDelay == otherDelay) {
return 0;
} else if (thisDelay > otherDelay) {
return 1;
} else {
return -1;
}
}
}
@Override
public int compareTo(Delayed other) {
DummyScheduledFuture otherTask = (DummyScheduledFuture) other;
if (this.value > otherTask.getValue()) {
return 1;
} else if (this.value < otherTask.getValue()) {
return -1;
} else {
return 0;
}
}
@Override public int compareTo(Delayed other) {
ScheduledTask otherTask = (ScheduledTask) other;
if (dueTimeNanos > otherTask.dueTimeNanos) {
return 1;
} else if (dueTimeNanos < otherTask.dueTimeNanos) {
return -1;
} else {
return 0;
}
}
@Override
public int compareTo(Delayed other) {
if (this == other) {
return 0;
}
long diff = getDelay(TimeUnit.MILLISECONDS) - other.getDelay(TimeUnit.MILLISECONDS);
return (diff == 0 ? 0 : ((diff < 0)? -1 : 1));
}
@Test
public void testWithSingleBoundParameterizedOnInstantiate() throws Exception {
Method bridgeMethod = DelayQueue.class.getMethod("add", Object.class);
assertTrue(bridgeMethod.isBridge());
Method actualMethod = DelayQueue.class.getMethod("add", Delayed.class);
assertFalse(actualMethod.isBridge());
assertEquals(actualMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod));
}
public int compareTo(Delayed o) {
if (System.identityHashCode(this) > System.identityHashCode(o))
return 1;
if (System.identityHashCode(this) < System.identityHashCode(o))
return -1;
return 0;
}
@Override
protected <V> ListenableScheduledFuture<V> schedule(Callable<V> callable, long delayInMillis) {
ListenableFutureTask<V> lft = new ListenableFutureTask<>(false, callable, singleThreadScheduler);
Delayed d = ThreadlyInternalAccessor.doScheduleAndGetDelayed(singleThreadScheduler,
lft, taskPriority, delayInMillis);
return new ScheduledFutureDelegate<>(lft, d);
}
@Override
public int compareTo(Delayed o) {
if (o.getDelay(TimeUnit.MILLISECONDS) < this.getDelay(TimeUnit.MILLISECONDS)) {
return 1;
} else if (o.getDelay(TimeUnit.MILLISECONDS) > this.getDelay(TimeUnit.MILLISECONDS)) {
return -1;
}
return 0;
}
@Override
public int compareTo(@Nonnull Delayed o) {
if (o == this) {
return 0;
}
long diff = getDelay(TimeUnit.NANOSECONDS) - o.getDelay(TimeUnit.NANOSECONDS);
return (diff < 0L) ? -1 : (diff > 0L) ? 1 : 0;
}
@Override
public int compareTo(Delayed o) {
if ((this.startTime - ((InFlightPacket) o).startTime) == 0) {
return 0;
}
if ((this.startTime - ((InFlightPacket) o).startTime) > 0) {
return 1;
} else {
return -1;
}
}
@Override
public int compareTo(Delayed o) {
if (!(o instanceof ContainerSimulator)) {
throw new IllegalArgumentException(
"Parameter must be a ContainerSimulator instance");
}
ContainerSimulator other = (ContainerSimulator) o;
return (int) Math.signum(endTime - other.endTime);
}
@Override
public int compareTo(Delayed o) {
Proxy element = (Proxy)o;
if (successfulAverageTime == 0.0d ||element.successfulAverageTime == 0.0d){
return 0;
}
return successfulAverageTime > element.successfulAverageTime ? 1:(successfulAverageTime < element.successfulAverageTime ? -1 : 0);
}
public int compareTo(Delayed d) {
TimerTaskList other = (TimerTaskList) d;
if (getExpiration() < other.getExpiration()) {
return -1;
} else if (getExpiration() > other.getExpiration()) {
return 1;
} else {
return 0;
}
}
public int compareTo(Delayed other) {
if (this == other) {
return 0;
}
long diff = getDelay(TimeUnit.MILLISECONDS) - other.getDelay(TimeUnit.MILLISECONDS);
return (diff == 0 ? 0 : ((diff < 0) ? -1 : 1));
}
@Override
public int compareTo(Delayed delayed) {
long me = getDelay(TimeUnit.MILLISECONDS);
long other = delayed.getDelay(TimeUnit.MILLISECONDS);
if (me < other) {
return -1;
}
if (me > other) {
return 1;
} else {
return 0;
}
}
@Override
// Note: this class has a natural ordering that is inconsistent with equals
public int compareTo(Delayed rhs)
{
if ( rhs == this )
{
return 0;
}
long diff = getDelay(TimeUnit.MILLISECONDS) - rhs.getDelay(TimeUnit.MILLISECONDS);
return (diff == 0) ? 0 : ((diff < 0) ? -1 : 1);
}
@Override
public int compareTo(Delayed o) {
Task that = (Task) o;
int c = Long.compare(time, that.time);
if (c != 0) {
return c;
}
return Integer.compare(System.identityHashCode(this), System.identityHashCode(that));
}
/**
* Compare the expiry time of this cache entry to another cache entry's expiry time.
*
* @param other a UGICache.Entry (passing any other kind of Delayed produces an error)
* @see java.lang.Comparable<>#compareTo(java.lang.Comparable<>)
*/
@Override
public int compareTo(Delayed other) {
if (!(other instanceof Entry)) return 1;
Entry that = (Entry) other;
return Long.compare(this.getDelayMillis(), that.getDelayMillis());
}
@Override
public int compareTo(Delayed o) {
if ((this.startTime - ((InFlightPacket) o).startTime) == 0) {
return 0;
}
if ((this.startTime - ((InFlightPacket) o).startTime) > 0) {
return 1;
} else {
return -1;
}
}
@Override
public int compareTo(Delayed o) {
if (!(o instanceof Task)) {
throw new IllegalArgumentException("Parameter must be a Task instance");
}
Task other = (Task) o;
return (int) Math.signum(nextRun - other.nextRun);
}