-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadline.java
More file actions
36 lines (32 loc) · 886 Bytes
/
Deadline.java
File metadata and controls
36 lines (32 loc) · 886 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
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
/**
* Represents a deadline for the player to make their move.
*/
public class Deadline {
private long deadline;
/**
* Gets CPU time in nanoseconds (but likely with millisecond or microsecond
* precision).
*/
public static long getCpuTime() {
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
return bean.isCurrentThreadCpuTimeSupported() ?
bean.getCurrentThreadCpuTime() : 0;
}
/**
* Constructs and sets the Deadline.
*
* @param deadline the deadline expressed in nanoseconds.
*/
public Deadline(long deadline) {
this.deadline = deadline;
}
/**
* Calculates and returns the remaining time until the Deadline must be met,
* in nanoseconds.
*/
long timeUntil() {
return deadline - Deadline.getCpuTime();
}
}