-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter.java
More file actions
60 lines (45 loc) · 1.53 KB
/
Copy pathCounter.java
File metadata and controls
60 lines (45 loc) · 1.53 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.time.Instant;
public class Counter {
private boolean isOn;
private long t0;
private long t;
private Instant timeReference;
public Counter(int t0){
this.t0 = t0;
t = t0;
isOn = true;
}
public long getT() {
return t;
}
public boolean isOn() {
return isOn;
}
public void openCounter() {
isOn = true;
}
public void closeCounter() {
isOn = false;
t0 = t;//语义其实不太对
}
public void resetT(int t) {
this.t = t;
}
public void changeReference(Instant instant){
timeReference = instant;
}
public void changeTime(Instant instant){
final long secondReference = timeReference.getEpochSecond();
final long temporarySecond = instant.getEpochSecond();
final long nanoReference = timeReference.getNano();
final long temporaryNano = instant.getNano();
final double DELTA_T = temporarySecond - secondReference +
(double)(temporaryNano - nanoReference)/1000000000;
t = t0 - (int)(DELTA_T);
}
//没有多线程的话,拨动秒表的操作必须在主线程里实现。
//通过Instant类。
//OpenCounter: 在切换后打开秒表,与Instant对准时间,依据Instant来倒计时。
//AdjustCounter:在循环过程中不断地与Instant进行对比,过一段时间后拨动一下秒表。
//倒计时结束后 throw TimeOver。
}