线程同步工具 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 ...
随机推荐
- HDU 5641
King's Phone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- MANIFEST.MF的文件的作用
在web项目中一个war包下面有一个文件叫:MANIFEST.MF 这个文件的作用是:告诉我们的信息有: Manifest-Version: 1.0Built-By: 张三(由谁创建)Build-Jd ...
- 实体框架(Entity Framework)快速入门--实例篇
在上一篇 <实体框架(Entity Framework)快速入门> 中我们简单了解的EF的定义和大体的情况,我们通过一步一步的做一个简单的实际例子来让大家对EF使用有个简单印象,看操作步骤 ...
- How to solve “Device eth0 does not seem to be present, delaying initialization” error
Today, I encountered with a strange error after I cloned CentOS 6 guest machine in Oracle VirtualBox ...
- mysql 字段为NULL的一些操作
1. 修改字段为NULL update tableName set column1 = null where id = 1 2. 字段是否为NULL (1)字段为空 select * tableNam ...
- photoshop的魔棒工具怎么用来抠图
魔棒工具是photoshop中提供的一种可以快速形成选区的工具,对于颜色边界分界明显的图片,能够一键形成选区,方便快捷. 本教程通过一个简单的实例,教新手怎么用Photoshop魔棒工具快速形成抠图选 ...
- Hadoop大数据生态系统及常用组件(山东数漫江湖)
经过多年信息化建设,我们已经进入一个神奇的“大数据”时代,无论是在通讯社交过程中使用的微信.QQ.电话.短信,还是吃喝玩乐时的用到的团购.电商.移动支付,都不断产生海量信息数据,数据和我们的工作生活密 ...
- 基于 python imageai 对象检测 目标检测 识别 视频
1.视频连接如下: http://www.iqiyi.com/w_19s6vownit.html
- JS的prototype和__proto__
一.prototype和__proto__的概念 prototype是函数的一个属性(每个函数都有一 个prototype属性),这个属性是一个指针,指向一个对象.它 是显示修改对象的原型的属性. _ ...
- Commonjs,AMD,CMD和UMD的差异
CommonJS 一种服务器端模块化的规范,Nodejs实现了这种规范,所以就说Nodejs支持CommonJS. CommonJS分为三部分: require 模块加载 exports 模块导出 m ...