-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSemphoreTest.java
More file actions
54 lines (45 loc) · 1.23 KB
/
Copy pathSemphoreTest.java
File metadata and controls
54 lines (45 loc) · 1.23 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
package java_base;
import java.util.concurrent.Semaphore;
import javax.swing.border.EmptyBorder;
class Client extends Thread{
private Semaphore semaphore;
public Client(String name, Semaphore semaphore) {
// TODO Auto-generated constructor stub
setName(name);
this.semaphore = semaphore;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println(getName() + "顾客排队中...");
try {
semaphore.acquire();
System.out.println(getName() + "接受任务中 ...");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println(getName() + "顾客离开.");
semaphore.release();
}
}
public class SemphoreTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Semaphore semaphore = new Semaphore(2);
Client p1 = new Client("A", semaphore);
Client p2 = new Client("B", semaphore);
Client p3 = new Client("C", semaphore);
p1.start();
p2.start();
p3.start();
}
}
/*
* 创建许可证 Semaphore semaphore = new Semaphore(2);
* 创建 private Semaphore semaphore;
* 在run方法里面存在 semaphore.acquire(); 如果没有拿到许可证的话这句相当于wait
* 归换许可证 semaphore.release();
*/