Java多线程:新特性—信号量

概念


Java的信号量实际上是一个功能完毕的计数器,对控制一定资源的消费与回收有着很重要的意义。

信号量常常用于多线程的代码中,并能监控有多少数目的线程等待获取资源,并且通过信号量可以得知可用资源的数目等等。

这里总是在强调“数目”二字,但不能指出来有哪些在等待,哪些资源可用。

例子


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
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* 信号量测试
*
*/
public class SemaphoreTest {
public static void main(String[] args) {
MyPool myPool = new MyPool(20);
// 创建线程池
ExecutorService threadPool = Executors.newFixedThreadPool(2);
MyT t1 = new MyT("任务A", myPool, 3);
MyT t2 = new MyT("任务B", myPool, 12);
MyT t3 = new MyT("任务C", myPool, 7);
// 在线程池中执行任务
threadPool.execute(t1);
threadPool.execute(t2);
threadPool.execute(t3);
// 关闭池
threadPool.shutdown();
}
}
/**
* 一个池
*/
class MyPool {
private Semaphore sp; // 池相关的信号量
/**
* 池的大小,这个大小会传递给信号量
*
* @param size
* 池的大小
*/
MyPool(int size) {
this.sp = new Semaphore(size);
}
public Semaphore getSp() {
return sp;
}
public void setSp(Semaphore sp) {
this.sp = sp;
}
}
class MyT extends Thread {
private String threadname; // 线程的名称
private MyPool pool; // 自定义池
private int x; // 申请信号量的大小
MyT(String threadname, MyPool pool, int x) {
this.threadname = threadname;
this.pool = pool;
this.x = x;
}
public void run() {
try {
// 从此信号量获取给定数目的许可
pool.getSp().acquire(x);
// todo:也许这里可以做更复杂的业务
System.out.println(threadname + "成功获取了" + x + "个许可!");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 释放给定数目的许可,将其返回到信号量。
pool.getSp().release(x);
System.out.println(threadname + "释放了" + x + "个许可!");
}
}
}

输出结果:

1
2
3
4
5
6
任务A成功获取了3个许可!
任务B成功获取了12个许可!
任务B释放了12个许可!
任务C成功获取了7个许可!
任务C释放了7个许可!
任务A释放了3个许可!