zjh
2023-10-12 8cde7ee1143bae70eb68d2b75f572d5b4dbadf98
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
41
42
43
44
45
46
47
package com.ltkj.common.task;
 
import com.google.common.primitives.Ints;
 
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
 
public abstract class Task implements Delayed, Runnable{
    private String id = "";
    private long start = 0;
 
    public Task(String id, long delayInMilliseconds){
        this.id = id;
        this.start = System.currentTimeMillis() + delayInMilliseconds;
    }
 
    public String getId() {
        return id;
    }
 
    @Override
    public long getDelay(TimeUnit unit) {
        long diff = this.start - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }
 
    @Override
    public int compareTo(Delayed o) {
        return Ints.saturatedCast(this.start - ((Task) o).start);
    }
 
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null) return false;
        if (!(o instanceof Task)) {
            return false;
        }
        Task t = (Task)o;
        return this.id.equals(t.getId());
    }
 
    @Override
    public int hashCode() {
        return this.id.hashCode();
    }
}