-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimer.java
36 lines (30 loc) · 929 Bytes
/
Timer.java
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
public class Timer implements Runnable{
private DisplayFrame frame;
private long timeStamp;
private final int SECOND = 1000000000;
public Timer(DisplayFrame frame){
this.frame = frame;
timeStamp = System.nanoTime()-1;
Thread t = new Thread(this);
t.start();
}
public void startTimer(int seconds){
timeStamp = 1 + System.nanoTime() + SECOND * ((long)seconds);
}
public void run(){
while(true){
if(System.nanoTime() < timeStamp + SECOND){
long currentTimeLeft = (timeStamp - System.nanoTime() + SECOND-1) / SECOND;
if(currentTimeLeft < frame.getTimeLeft()){
frame.decreaseTimer();
}
}
try{
Thread.sleep(15);
}
catch(Exception e){
return;
}
}
}
}