-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowTest.java
More file actions
38 lines (32 loc) · 985 Bytes
/
Copy pathWindowTest.java
File metadata and controls
38 lines (32 loc) · 985 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
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 WindowTest {
public static void main(String[] args) {
WindowThread window1 = new WindowThread();
WindowThread window2 = new WindowThread();
WindowThread window3 = new WindowThread();
window1.setName("窗口一");
window2.setName("窗口二");
window3.setName("窗口三");
window1.start();
window2.start();
window3.start();
}
}
class WindowThread extends Thread{
private static int tickets = 100; // make it static to be shared with all instances
@Override
public void run() {
while(true){
if(tickets > 0){
System.out.println(getName() + " 售出票号: " + tickets);
tickets --;
}else{
break;
}
}
}
}