-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyThread1.java
More file actions
52 lines (41 loc) · 1008 Bytes
/
MyThread1.java
File metadata and controls
52 lines (41 loc) · 1008 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
46
47
48
49
50
51
52
package org.cp;
/**
* create by CP on 2019/7/22 0022.
*/
public class MyThread1 implements Runnable {
Integer ticket = 100;
@Override
public void run() {
while (ticket >= 0) {
sell();
}
}
synchronized void sell() {
synchronized (this.ticket) {
System.out.println(Thread.currentThread().getName() + ": " + ticket);
ticket--;
}
}
}
class Test1 {
public static void main(String[] args) {
MyThread1 t = new MyThread1();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Runnable r = () -> {
while (t.ticket >= 0) {
t.sell();
}
};
Thread t4 = new Thread(r);
t1.setName("窗口1 ");
t2.setName("窗口2 ");
t3.setName("窗口3 ");
t4.setName("窗口4 ");
t1.start();
t2.start();
t3.start();
// t4.start();
}
}