线程同步工具 Semaphore类使用案例
参考博文 :
使用Semaphore模拟互斥锁
当一个线程想要访问某个共享资源,首先,它必须获得semaphore。如果semaphore的内部计数器的值大于0,那么semaphore减少计数器的值并允许访问共享的资源。计数器的值大于0表示,有可以自由使用的资源,所以线程可访问并使用它们。
另一种情况,如果semaphore的计数器的值等于0,那么semaphore让线程进入休眠状态一直到计数器大于0。计数器的值等于0表示全部的共享资源都正被线程们使用,所以线程想要访问就必须等到某个资源成为自由的。
当线程使用完共享资源时,他就必须释放出semaphore,增加semaphore的内部计数器的值,让其他线程可以访问共享资源。
- 方法:
设置Semaphore的permits大小为1,这样同一个时刻,只能有一个线程,进入acquire和release保护的方法体内。 - Code:
import java.util.concurrent.Semaphore;
//1. 使用semaphore保护的互斥打印队列
class PrintQueue {
private final Semaphore semaphore;
public PrintQueue() {
// 只设定一个许可,这样同一个时刻 只能一个线程执行 printJob方法,从而实现互斥锁
semaphore = new Semaphore(1);
}
//2.实现Implement the printJob()方法,此方法可以模拟打印文档,并接收document对象作为参数。
public void printJob(Object document) {
try {
semaphore.acquire();
//3.然后,实现能随机等待一段时间的模拟打印文档的行。
long duration = (long) (Math.random() * 10);
System.out.printf("%s: PrintQueue: Printing a Job during %d seconds\n", Thread.currentThread().getName(), duration);
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//7.最后,释放semaphore通过调用semaphore的release()方法。
semaphore.release();
System.out.printf("%s: PrintQueue: Printing a Job release the permits \n", Thread.currentThread().getName());
}
}
}
// 模拟打印进程
class PrintThread extends Thread {
PrintQueue printQueue ;
public PrintThread (PrintQueue printQueue) {
this.printQueue = printQueue ;
}
@Override
public void run() {
printQueue.printJob(new Object());
}
}
public class PrintQueueTest {
public static void main(String[] args) {
PrintQueue printQueue = new PrintQueue() ;
PrintThread a = new PrintThread(printQueue) ;
a.setName("A");
PrintThread b = new PrintThread(printQueue);
b.setName("B");
PrintThread c = new PrintThread(printQueue) ;
c.setName("C");
a.start();
b.start();
c.start();
}
}
控制并发访问多个资源
在上面的例子中,我们使用了semaphore来保护访问一个共享资源的,或者说一个代码片段每次只能被一个线程执行。但是semaphore也可以用来保护多个资源的副本,也就是说当你有一个代码片段每次可以被指定数量的多个线程执行时,可以考虑使用Semaphore。
下面的例子会有一个print queue 但可以在3个不同的打印机上打印文件。
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class PrintQueue2 {
// 模拟打印机 数组
private boolean freePrinters[];
private Lock lockPrinters;
private final Semaphore semaphore;
public PrintQueue2() {
semaphore = new Semaphore(2);
//模拟 初始化三个可用的打印机
freePrinters = new boolean[]{true,true,true};
lockPrinters = new ReentrantLock();
}
public void printJob(Object document) {
int assignedPrinter = -1 ;
try {
semaphore.acquire();
// 获取可用的打印机 序号
assignedPrinter = getPrinter();
//7.然后, 随机等待一段时间来实现模拟打印文档的行。
long duration = (long) (Math.random() * 10);
System.out.printf("%s: PrintQueue: Printing a Job in Printer%d during %d seconds\n", Thread.currentThread().getName(), assignedPrinter, duration);
TimeUnit.SECONDS.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if(assignedPrinter != -1) { // 模拟释放打印机
freePrinters[assignedPrinter] = true;
}
semaphore.release();
System.out.printf("%s: PrintQueue: 打印结束释放打印机\n", Thread.currentThread().getName());
}
}
// 遍历找到当前可用的 打印机的索引下标
private int getPrinter() {
int ret = -1;
try {
lockPrinters.lock();
for (int i = 0; i < freePrinters.length; i++) {
if (freePrinters[i]) {
ret = i;
freePrinters[i] = false;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lockPrinters.unlock();
}
return ret;
}
}
// 模拟打印进程
class PrintThread extends Thread {
PrintQueue2 printQueue2 ;
public PrintThread (PrintQueue2 printQueue) {
this.printQueue2 = printQueue ;
}
@Override
public void run() {
printQueue2.printJob(new Object());
}
}
class PrintQueueTest {
public static void main(String[] args) {
PrintQueue2 printQueue = new PrintQueue2() ;
PrintThread a = new PrintThread(printQueue) ;
a.setName("A");
PrintThread b = new PrintThread(printQueue);
b.setName("B");
PrintThread c = new PrintThread(printQueue) ;
c.setName("C");
a.start();
b.start();
c.start();
}
}
使用Semaphore控制并发线程数
Semaphore可以用来做流量控制,特别公用资源有限的应用场景,比如数据库连接。假设有一个需求,要读取几万个文件的数据,因为都是IO密集型任务,我们可以启动几十个线程并发的读取,但是如果读到内存后,还需要进行存储到数据库中,而数据库的连接数只有10几个,这时我们必须控制只有十个线程同时获取数据库连接保存数据,否则会报错无法获取数据库连接。这个时候,我们就可以使用Semaphore来做流控。
public class SemaphoreTest2 {
private static final int THREAD_COUNT = 30;
private static ExecutorService threadPool = Executors
.newFixedThreadPool(THREAD_COUNT);
private static Semaphore semaphore = 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 {
semaphore.acquire();
System.out.println("save data");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
}
});
}
threadPool.shutdown();
}
}
在代码中,虽然有30个线程正在执行,但是只允许10个并发的执行。Semaphore的构造方法Semaphore(int permits)接收一个整型的数字,表示可用的许可证数量。Semaphore(10)表示允许10个线程获取许可证,也就是最大并发数是10。
线程同步工具 Semaphore类使用案例的更多相关文章
- 线程同步工具 Semaphore类的基础使用
推荐好文: 线程同步工具(一) 线程同步工具(二)控制并发访问多个资源 并发工具类(三)控制并发线程数的Semaphore 简介 Semaphore是基于计数的信号量,可以用来控制同时访问特定资源的线 ...
- 【java并发】线程同步工具Semaphore的使用
Semaphore通常用于限制可以访问某些资源(物理或逻辑的)的线程数目,我们可以自己设定最大访问量.它有两个很常用的方法是acquire()和release(),分别是获得许可和释放许可. 官方J ...
- Java核心知识点学习----线程同步工具类,CyclicBarrier学习
线程同步工具类,CyclicBarrier日常开发较少涉及,这里只举一个例子,以做备注.N个人一块出去玩,相约去两个地方,CyclicBarrier的主要作用是等待所有人都汇合了,才往下一站出发. 1 ...
- 经典线程同步 信号量Semaphore
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...
- 秒杀多线程第八篇 经典线程同步 信号量Semaphore
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <且不超过最大资源数量. 第三个參数能够用来传出先前的资源计数,设为NULL表示不须要传出. 注意:当 ...
- 多线程面试题系列(8):经典线程同步 信号量Semaphore
前面介绍了关键段CS.事件Event.互斥量Mutex在经典线程同步问题中的使用.本篇介绍用信号量Semaphore来解决这个问题. 首先也来看看如何使用信号量,信号量Semaphore常用有三个函数 ...
- 转---秒杀多线程第八篇 经典线程同步 信号量Semaphore
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...
- linux系统编程:线程同步-信号量(semaphore)
线程同步-信号量(semaphore) 生产者与消费者问题再思考 在实际生活中,仅仅要有商品.消费者就能够消费,这没问题. 但生产者的生产并非无限的.比如,仓库是有限的,原材料是有限的,生产指标受消费 ...
- 多线程状态与优先级、线程同步与Monitor类、死锁
一.线程状态 二.线程优先级 三.初步尝试多线程 class Program { static void Main(string[] args) { while (true) { MessagePri ...
随机推荐
- [usaco] 2008 Dec Largetst Fence 最大的围栏 2 || dp
原网站大概已经上不了了-- 题目大意: 求出平面上n个点组成的一个包含顶点数最多的凸多边形.n<=250. 考虑我们每次枚举凸包的左下角为谁(参考Graham求凸包时的左下角),然后像Graha ...
- 快速搭建http服务:共享文件--Java的我,不知Python你的好
在 Linux 服务器上或安装了 Python 的机器上, 我们可以在指定的文件目录下,使用 python -m SimpleHTTPServer 快速搭建一个http服务,提供一个文件浏览的web ...
- bzoj1045: [HAOI2008] 糖果传递(思维题)
首先每个人一定分到的糖果都是所有糖果的平均数ave. 设第i个人给i-1个人Xi个糖果,则有Ai-Xi+X(i+1)=ave. 则A1-X1+X2=ave,A2-X2+X3=ave,A3-X3+X4= ...
- PC蓝牙通信C#代码实现
PC蓝牙通信C#代码实现 这篇文章主要为大家详细介绍了PC蓝牙通信C#代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了C#实现PC蓝牙通信代码,供大家参考,具体内容如下 ...
- mysql的时间函数整理
转:这里总结的非常齐全: http://fengbin2005.iteye.com/blog/1999763 Mysql时间函数 对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描 ...
- HDU2732 最大流
Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- job源码分析
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agree ...
- CSS hack浏览器兼容一览表
CSS hack是指我们为了兼容各浏览器,而使用的特别的css定义技巧.这是国外摘来的一张CSS hack列表,显示了各浏览器对css hack的支持程度,对我们制作兼容网页非常有帮助.
- 【BZOJ4069】【APIO2015】巴厘岛的雕塑 [贪心][DP]
巴厘岛的雕塑 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 印尼巴厘岛的公路上有许多的雕塑, ...
- [BZOJ1177][BZOJ1178][BZOJ1179]APIO2009解题报告
抱着好奇心态去开始做APIO的往年试题感受一下难度 Oil Description 采油区域 Siruseri政府决定将石油资源丰富的Navalur省的土地拍卖给私人承包商以建立油井.被拍卖的整块土地 ...