-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimer.java
56 lines (50 loc) · 1.28 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.net.DatagramSocket;
import java.net.SocketException;
class Timer extends Thread {
public static final int rate = 5; // 5 milliseconds
private Sender sender;
private long duration;
private long elapsed;
private boolean stop;
private boolean isRunning;
public Timer(Sender s, long duration) {
this.duration = duration;
sender = s;
elapsed = 0;
stop = false;
isRunning = false;
}
public synchronized void stopTimer() {
stop = true;
isRunning = false;
}
public void run() {
isRunning = true;
while (!stop) {
if (Thread.interrupted())
return;
try {
Thread.sleep(rate);
} catch (InterruptedException ioe) {
continue;
}
synchronized ( this ) {
elapsed += rate;
if (elapsed > duration) {
timeout();
break;
}
}
}
}
public boolean isRunning() {
return isRunning;
}
public void timeout() {
stopTimer();
try {
sender.retransmit(true);
} catch (NullPointerException ne) {
}
}
}