-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowTest3plus.java
More file actions
63 lines (48 loc) · 1.65 KB
/
Copy pathWindowTest3plus.java
File metadata and controls
63 lines (48 loc) · 1.65 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
57
58
59
60
package ThreadTest.java;
/* 使用同步方法的方式来做线程同步安全处理 ,
* 在实现 Runnable 接口中应用 synchronized method(){...}, the lock is 'this' by default
* 仍然需要同步监视器(🔐),只是被隐藏了,实际是 Window3plus.class
* non static scynchronized method --> this
* static synchronized --> 当前类本身
*
*
*/
public class WindowTest3plus {
public static void main(String[] args) {
Window3plus window1 = new Window3plus();
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 Window3plus implements Runnable{
public Window3plus() {
}
private int tickets = 100; // make it static to be shared with all instances
@Override
public void run() {
while(true){
sell();
}
}
private synchronized void sell(){
// 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 --;
}
// }
}
}