参考博文 :

  1. 线程同步工具(一)
  2. 线程同步工具(二)控制并发访问多个资源
  3. 并发工具类(三)控制并发线程数的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类使用案例的更多相关文章

  1. 线程同步工具 Semaphore类的基础使用

    推荐好文: 线程同步工具(一) 线程同步工具(二)控制并发访问多个资源 并发工具类(三)控制并发线程数的Semaphore 简介 Semaphore是基于计数的信号量,可以用来控制同时访问特定资源的线 ...

  2. 【java并发】线程同步工具Semaphore的使用

    Semaphore通常用于限制可以访问某些资源(物理或逻辑的)的线程数目,我们可以自己设定最大访问量.它有两个很常用的方法是acquire()和release(),分别是获得许可和释放许可.  官方J ...

  3. Java核心知识点学习----线程同步工具类,CyclicBarrier学习

    线程同步工具类,CyclicBarrier日常开发较少涉及,这里只举一个例子,以做备注.N个人一块出去玩,相约去两个地方,CyclicBarrier的主要作用是等待所有人都汇合了,才往下一站出发. 1 ...

  4. 经典线程同步 信号量Semaphore

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  5. 秒杀多线程第八篇 经典线程同步 信号量Semaphore

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <且不超过最大资源数量. 第三个參数能够用来传出先前的资源计数,设为NULL表示不须要传出. 注意:当 ...

  6. 多线程面试题系列(8):经典线程同步 信号量Semaphore

    前面介绍了关键段CS.事件Event.互斥量Mutex在经典线程同步问题中的使用.本篇介绍用信号量Semaphore来解决这个问题. 首先也来看看如何使用信号量,信号量Semaphore常用有三个函数 ...

  7. 转---秒杀多线程第八篇 经典线程同步 信号量Semaphore

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  8. linux系统编程:线程同步-信号量(semaphore)

    线程同步-信号量(semaphore) 生产者与消费者问题再思考 在实际生活中,仅仅要有商品.消费者就能够消费,这没问题. 但生产者的生产并非无限的.比如,仓库是有限的,原材料是有限的,生产指标受消费 ...

  9. 多线程状态与优先级、线程同步与Monitor类、死锁

    一.线程状态 二.线程优先级 三.初步尝试多线程 class Program { static void Main(string[] args) { while (true) { MessagePri ...

随机推荐

  1. cf 460 E. Congruence Equation 数学题

    cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...

  2. bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)

    这题之前写过线段树合并,今天复习Splay的时候想起这题,打算写一次Splay+启发式合并. 好爽!!! 写了长长的代码(其实也不长),只凭着下午的一点记忆(没背板子...),调了好久好久,过了样例, ...

  3. NDK plugin来构建JNI项目(相对于手动构建)

    http://blog.csdn.net/codezjx/article/details/8879670 1.添加ndk环境支持 Android Tools -> Add Native Supp ...

  4. STL之二:vector容器用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8507394 vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组. ...

  5. SDWebImage的使用说明

    1. 在需要的地方导入头文件 #import "UIImageView+WebCache.h" webCache:网络缓存,几乎目前所有的浏览器都有一个内置的缓存,它们通常利用客户 ...

  6. POJ 3579 二分

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7687   Accepted: 2637 Descriptio ...

  7. 网页导出excel文件

    response.setContentType("application/vnd.ms-excel"); response.setHeader("content-disp ...

  8. Mybatis(3) 映射文件-增删改查

    映射文件: 映射文件是根据数据库模型生成的编写sql脚本xml文件, mapper标签中namespace属性值为对应模型实体类的全类名. <?xml version="1.0&quo ...

  9. 知问前端——Ajax提交表单

    本文,运用两大表单插件,完成数据表新增的工作. 一.创建数据库 创建一个数据库,名称为:zhiwen,表——user表,字段依次为:id.name.pass.email.sex.birthday.da ...

  10. fs.createReadStream(filepath).pipe(response);这句是什么意思?

    'use strict'; var fs = require('fs'), url = require('url'), path = require('path'), http = require(' ...