-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWindow2_2.java
More file actions
56 lines (51 loc) · 1.31 KB
/
Window2_2.java
File metadata and controls
56 lines (51 loc) · 1.31 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
package org.cp;
/**
* 继承方式线程
* 同步方法 线程安全
* create by CP on 2019/7/25 0025.
*/
public class Window2_2 implements Runnable {
private static int ticket = 100;
private static Object o = new Object();
@Override
public void run() {
while (true) {
if (sell()) {
break;
}
}
}
/**
* 这里不用要声明静态
* @return boolean
*/
// private static synchronized boolean sell() {
private synchronized boolean sell() {
if (ticket>0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+ ": "+ticket);
ticket--;
} else {
return true;
}
return false;
}
}
class WindowTest2_2 {
public static void main(String[] args) {
Window2_2 window = new Window2_2();
Thread window1 = new Thread(window);
Thread window2 = new Thread(window);
Thread window3 = new Thread(window);
window1.setName("窗口1 ");
window2.setName("窗口2 ");
window3.setName("窗口3 ");
window1.start();
window2.start();
window3.start();
}
}