-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowTest2.java
More file actions
44 lines (35 loc) · 1019 Bytes
/
Copy pathWindowTest2.java
File metadata and controls
44 lines (35 loc) · 1019 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
37
38
39
40
41
42
43
44
package ThreadTest.java;
/*
* 1. create 3 windows selling tickets
* 2. there are tree windows with same ticket num: 100, is a security issue, need to be resolved.
* */
public class WindowTest2 {
public static void main(String[] args) {
Window2 window1 = new Window2();
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 Window2 implements Runnable{
public Window2() {
}
private int tickets = 100; // make it static to be shared with all instances
@Override
public void run() {
while(true){
if(tickets > 0){
System.out.println(Thread.currentThread().getName() + " 售出票号: " + tickets);
tickets --;
}else{
break;
}
}
}
}