-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.java
More file actions
59 lines (53 loc) · 1.56 KB
/
Timer.java
File metadata and controls
59 lines (53 loc) · 1.56 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
public class Timer extends Thread {
private int time = 0;
private static boolean setting = false;
public synchronized int getTime() {
while (setting) {
try {
wait();
} catch (InterruptedException e) {
}
}
return time;
}
public synchronized void setTime(int t) {
this.time = t;
setting = false;
notifyAll();
}
public void run() {
while (true) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(time + " ");
setTime(time + 1);
}
}
}
class MessageTimer5 extends Thread{
Timer timer;
public MessageTimer5(Timer t){
this.timer = t;
}
public void run(){
synchronized(timer){
while(true){
try {timer.wait();} catch (InterruptedException e) {e.printStackTrace();}
if(timer.getTime() % 5 == 0){
System.out.print("\nМинуло 5 секунд\n");
}
}
}
}
}
class TimerTester {
public static synchronized void main(String[] args) {
Timer timer = new Timer();
timer.start();
MessageTimer5 t1 = new MessageTimer5(timer);
t1.start();
}
}