计数信号量

Semaphore的acquire方法用于确保存在可用资源。当存在可用资源时,线程会立即从acquire方法返回,同时信号量内部的资源个数会减1.如无可用资源,线程则阻塞在acquire方法内,直至出现可用资源。
Semaphore的release方法用于释放资源。释放资源后,信号量内部的资源个数会增加1。另外,如果acquire中存在等待的线程,

示例

多个线程使用数量有限的资源。

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
61
62
63
64
import java.util.Random;
import java.util.concurrent.Semaphore;

public class Main {
public static void main(String[] args) {
BoundedResource resource = new BoundedResource(3);

for (int i = 0; i < 10; i++) {
new UserThread(resource).start();
}
}
}

class UserThread extends Thread {
private final BoundedResource resource;
private final static Random random = new Random(26535);

public UserThread(BoundedResource resource) {
this.resource = resource;
}

public void run() {
try {
while (true) {
resource.use();
Thread.sleep(random.nextInt(3000));
}
} catch (InterruptedException e) {
}
}
}

class BoundedResource {
private final int permits;
private final Semaphore semaphore;
private final static Random random = new Random(314159);

public BoundedResource(int permits) {
this.semaphore = new Semaphore(permits);
this.permits = permits;
}

public void use() throws InterruptedException {
semaphore.acquire();

try {
doUse();
} finally {
semaphore.release();
}
}

protected void doUse() throws InterruptedException {
Log.println("BEGIN: used = " + (permits - semaphore.availablePermits()));
Thread.sleep(random.nextInt(500));
Log.println("END: used = " + (permits - semaphore.availablePermits()));
}
}

class Log {
public static void println(String s) {
System.out.println(Thread.currentThread().getName() + ": " + s);
}
}

运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Thread-0: BEGIN: used = 1
Thread-1: BEGIN: used = 2
Thread-2: BEGIN: used = 3
Thread-1: END: used = 3
Thread-3: BEGIN: used = 3
Thread-3: END: used = 3
Thread-4: BEGIN: used = 3
Thread-2: END: used = 3
Thread-5: BEGIN: used = 3
Thread-4: END: used = 3
Thread-6: BEGIN: used = 3
Thread-6: END: used = 3
Thread-7: BEGIN: used = 3
Thread-0: END: used = 3
Thread-8: BEGIN: used = 3
Thread-5: END: used = 3
Thread-9: BEGIN: used = 3
Thread-8: END: used = 3
Thread-9: END: used = 2
Thread-1: BEGIN: used = 2