-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathDelayMsg.java
More file actions
executable file
·40 lines (36 loc) · 940 Bytes
/
DelayMsg.java
File metadata and controls
executable file
·40 lines (36 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package ca.mcmaster.queue.arrayblockingqueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class DelayMsg<V> implements Delayed{
private Long expire;
private Long insertTime;
private V v;
public V getV() {
return v;
}
public Long getInsertTime() {
return insertTime;
}
public DelayMsg(Long expire, Long insertTime, V v){
this.expire = expire;
this.insertTime = insertTime;
this.v = v;
}
@Override
public int compareTo(Delayed o) {
@SuppressWarnings("rawtypes")
Long delta = insertTime - ((DelayMsg)o).getInsertTime();
if(delta < 0) return -1;
else if(delta == 0) return 0;
else
return 1;
}
/* (non-Javadoc)
* @see java.util.concurrent.Delayed#getDelay(java.util.concurrent.TimeUnit)
* 用于定义延时策略
*/
@Override
public long getDelay(TimeUnit unit) {
return unit.convert(this.expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
}