版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/u010647035/article/details/86667630
1、概述
Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源,就是控制并发线程的数量
2、类图
Semaphore还提供一些其他方法:
int availablePermits() : 返回此信号量中当前可用的许可证数。
int getQueueLength(): 返回正在等待获取许可证的线程数。
boolean hasQueuedThreads() : 是否有线程正在等待获取许可证。
void reducePermits(int reduction) : 减少reduction个许可证。是个protected方法。
Collection getQueuedThreads() :返回所有等待获取许可证的线程集合。是个protected方法。
3、适用场景
Semaphore可以用于做流量控制,特别公用资源有限的应用场景,比如数据库连接。假如有一个需求,要读取几万个文件的数据,因为都是IO密集型任务,我们可以启动几十个线程并发的读取,但是如果读到内存后,还需要存储到数据库中,而数据库的连接数只有10个,这时我们必须控制只有十个线程同时获取数据库连接保存数据,否则会报错无法获取数据库连接。这个时候,我们就可以使用Semaphore来做流控
4、代码示例
public class SemaphoreTest {
private static final int THREAD_COUNT = 30;
private static ExecutorService threadPool = newFixedThreadPool(THREAD_COUNT);
private static Semaphore s = new Semaphore(10);
public static void main(String[] args) {
for (int i = 0; i < THREAD_COUNT; i++) {
threadPool.execute(new Runnable() {
@Override
public void run() {
try {
s.acquire();
System.out.println("save data");
s.release();
} catch (InterruptedException e) {
}
}
});
}
threadPool.shutdown();
}
}