-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowTest4.java
More file actions
45 lines (33 loc) · 862 Bytes
/
WindowTest4.java
File metadata and controls
45 lines (33 loc) · 862 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
45
package Thread;
//同步方法解决同步问题
class Window4 implements Runnable{
private int ticket = 100;
@Override
public void run() {
while(true){
show();
}
}
private synchronized void show(){
if(ticket > 0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + ticket);
ticket--;
}
}
}
public class WindowTest4 {
public static void main(String[] args) {
Window4 w4 = new Window4();
Thread t1 = new Thread(w4);
Thread t2 = new Thread(w4);
Thread t3 = new Thread(w4);
t1.start();
t2.start();
t3.start();
}
}