-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowTest3.java
More file actions
52 lines (40 loc) · 1.34 KB
/
Copy pathWindowTest3.java
File metadata and controls
52 lines (40 loc) · 1.34 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
package ThreadTest.java;
public class WindowTest3 {
public static void main(String[] args) {
Window3 window1 = new Window3();
Thread w1 = new Thread(window1);
Thread w2 = new Thread(window1);
Thread w3 = new Thread(window1);
w1.setName("窗口一");
w2.setName("窗口二");
w3.setName("窗口三");
w1.start();
w2.start();
w3.start();
}
}
class Window3 implements Runnable{
public Window3() {
}
private int tickets = 100; // make it static to be shared with all instances
Object obj = new Object();
@Override
public void run() {
while(true){
synchronized (this){ // more simple way: use 'this'
// synchronized (obj){ // the lock can be shared among threads if there is shared data, and the lock should be exactly the same for those threads.
if(tickets > 0){
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println(Thread.currentThread().getName() + " 售出票号: " + tickets);
tickets --;
}else{
break;
}
}
}
}
}