前面我们介绍了2种同步机制:

1)使用synchronized关键字

2)使用Lock接口及其实现类:

ReentrantLock,ReentrantReadWriteLock.ReadLock, and ReentrantReadWriteLock.WriteLock

本例中,我们将学习更高级的同步方式:semaphore (信号量)

先看下概念:

Semaphores: A semaphore is a counter that controls the access to one ormore shared resources. This mechanism is one of the basic tools of concurrent programming and is provided by most of the programming languages.

一个信号量是控制读取一个或多个共享资源的计数器。

这一机制是并发编程中提供的一种基本工具并且绝大多数编程语言都有提供。

当一个线程想去读取某个共享资源,它必须获得信号。

如果semaphore大于0,这意味着当前资源处于空闲状态,这个时候它会减1,并允许该线程读取。



否则,信号将会让该线程睡眠直到信号量再次大于0。

当线程使用完共享资源后需要释放信号量,这样其它线程方能接着获取。

PrintQueue.java
package com.dylan.thread.ch3.c01.task;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit; /**
* This class implements the PrintQueue using a Semaphore to control the
* access to it.
*
*/
public class PrintQueue { /**
* Semaphore to control the access to the queue
*/
private final Semaphore semaphore; /**
* Constructor of the class. Initializes the semaphore
*/
public PrintQueue(){
semaphore=new Semaphore(1);
} /**
* Method that simulates printing a document
* @param document Document to print
*/
public void printJob (Object document){
try {
// Get the access to the semaphore. If other job is printing, this
// thread sleep until get the access to the semaphore
semaphore.acquire(); 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);
TimeUnit.SECONDS.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Free the semaphore. If there are other threads waiting for this semaphore,
// the JVM selects one of this threads and give it the access.
semaphore.release();
}
} }
Job.java
package com.dylan.thread.ch3.c01.task;

/**
* This class simulates a job that send a document to print.
*
*/
public class Job implements Runnable { /**
* Queue to print the documents
*/
private PrintQueue printQueue; /**
* Constructor of the class. Initializes the queue
* @param printQueue
*/
public Job(PrintQueue printQueue){
this.printQueue=printQueue;
} /**
* Core method of the Job. Sends the document to the print queue and waits
* for its finalization
*/
@Override
public void run() {
System.out.printf("%s: Going to print a job\n",Thread.currentThread().getName());
printQueue.printJob(new Object());
System.out.printf("%s: The document has been printed\n",Thread.currentThread().getName());
}
}
Main.java
package com.dylan.thread.ch3.c01.core;

import com.dylan.thread.ch3.Job;
import com.dylan.thread.ch3.PrintQueue; /**
* Main class of the example.
*
*/
public class Main { /**
* Main method of the class. Run ten jobs in parallel that
* send documents to the print queue at the same time.
*/
public static void main (String args[]){ // Creates the print queue
PrintQueue printQueue=new PrintQueue(); // Creates ten Threads
Thread thread[]=new Thread[10];
for (int i=0; i<10; i++){
thread[i]=new Thread(new Job(printQueue),"Thread "+i);
} // Starts the Threads
for (int i=0; i<10; i++){
thread[i].start();
}
} }

运行结果:

Thread 3: Going to print a job
Thread 9: Going to print a job
Thread 5: Going to print a job
Thread 7: Going to print a job
Thread 4: Going to print a job
Thread 0: Going to print a job
Thread 2: Going to print a job
Thread 1: Going to print a job
Thread 8: Going to print a job
Thread 6: Going to print a job
Thread 3: PrintQueue: Printing a Job during 2 seconds
Thread 9: PrintQueue: Printing a Job during 5 seconds
Thread 3: The document has been printed
Thread 9: The document has been printed
Thread 5: PrintQueue: Printing a Job during 9 seconds
Thread 5: The document has been printed
Thread 7: PrintQueue: Printing a Job during 7 seconds
Thread 7: The document has been printed
Thread 4: PrintQueue: Printing a Job during 9 seconds
Thread 0: PrintQueue: Printing a Job during 6 seconds
Thread 4: The document has been printed
Thread 0: The document has been printed
Thread 2: PrintQueue: Printing a Job during 5 seconds
Thread 2: The document has been printed
Thread 1: PrintQueue: Printing a Job during 1 seconds
Thread 8: PrintQueue: Printing a Job during 5 seconds
Thread 1: The document has been printed
Thread 8: The document has been printed
Thread 6: PrintQueue: Printing a Job during 3 seconds
Thread 6: The document has been printed

Java并发编程实例--20.使用Semaphores(信号量)控制资源的并发读取的更多相关文章

  1. 【Java并发编程】6、volatile关键字解析&内存模型&并发编程中三概念

    volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...

  2. Python并发编程实例教程

    有关Python中的并发编程实例,主要是对Threading模块的应用,文中自定义了一个Threading类库. 一.简介 我们将一个正在运行的程序称为进程.每个进程都有它自己的系统状态,包含内存状态 ...

  3. Java并发编程系列-(9) JDK 8/9/10中的并发

    9.1 CompletableFuture CompletableFuture是JDK 8中引入的工具类,实现了Future接口,对以往的FutureTask的功能进行了增强. 手动设置完成状态 Co ...

  4. java多线程编程实例

    [转]这篇文章主要介绍了java多线程编程实例,分享了几则多线程的实例代码,具有一定参考价值,加深多线程编程的理解还是很有帮助的,需要的朋友可以参考下. 1.三个售票窗口同时出售20张票程序分析:   ...

  5. C#并发编程实例讲解-概述(01)

    在工作中经常遇到需要并发编程的实例,一直没有时间来整理,现在空了下来,个人整理对并发一下理解. 关于并发编程的几个误解 误解一:并发编程就是多线程 实际上多线只是并发编程的一中形式,在C#中还有很多更 ...

  6. Java并发编程实例(synchronized)

    此处用一个小程序来说明一下,逻辑是一个计数器(int i):主要的逻辑功能是,如果同步监视了资源i,则不输出i的值,但如果没有添加关键字synchronized,因为是两个线程并发执行,所以会输出i的 ...

  7. Java并发编程原理与实战三十三:同步容器与并发容器

    1.什么叫容器? ----->数组,对象,集合等等都是容器.   2.什么叫同步容器? ----->Vector,ArrayList,HashMap等等.   3.在多线程环境下,为什么不 ...

  8. ~~并发编程(十三):信号量,Event,定时器~~

    进击のpython ***** 并发编程--信号量,Event,定时器 本节需要了解的就是: 信号量,以及信号量和互斥锁的区别 了解时间和定时器,以及使用 信号量 信号量也是锁,本质没有变!但是他跟互 ...

  9. 《Java并发编程的艺术》读书笔记:一、并发编程的目的与挑战

    发现自己有很多读书笔记了,但是一直都是自己闷头背,没有输出,突然想起还有博客圆这么个好平台给我留着位置,可不能荒废了. 此文读的书是<Jvava并发编程的艺术>,方腾飞等著,非常经典的一本 ...

  10. [笔记][Java7并发编程实战手冊]3.4 等待多个并发事件的完毕CountDownLatch倒计数闭锁

    [笔记][Java7并发编程实战手冊]系列文件夹 简单介绍 本文学习CountDownLatch 倒计数闭锁. 本人英文不好.靠机器翻译,然后有一段非常形象的描写叙述,让我把它叫为倒计数 用给定的计数 ...

随机推荐

  1. [转帖]探索惊群 ⑥ - nginx - reuseport

    https://wenfh2020.com/2021/10/12/thundering-herd-tcp-reuseport/   SO_REUSEPORT (reuseport) 是网络的一个选项设 ...

  2. [转帖]jmeter 使用beanshell 编写脚本

    目录 一.介绍 1.1 介绍 1.2 下载&启动 二.jmeter中创建beanshell脚本 三.jmeter与beanshell 数据交互 3.1 例子1 beanshell 将变量传给j ...

  3. [转帖]优化命令之iotop命令

    文章目录 引言 一.iotop简介 1.iotop安装 2.iotop语法 3.iotop参数 二.I/O的常用快捷键 三.交互模式 四.iotop示例 1.只显示正在产生I/O的进程 2.显示指定P ...

  4. [转帖]Perf IPC以及CPU性能

    https://plantegg.github.io/2021/05/16/Perf%20IPC%E4%BB%A5%E5%8F%8ACPU%E5%88%A9%E7%94%A8%E7%8E%87/ Pe ...

  5. 部署于K8S集群上面应用性能影响点推测

    前言 本人2017年第一次接触K8S. 中间断断续续学习K8S相关的内容. 但是最近一年,几乎没太有学习. 因为之前学习了四五年, 一直以为产品马上要用 结果一直被浇冷水. 去年开始学乖了. 不这么搞 ...

  6. UOS关闭激活提示: Your system is not activated. Please activate as soon as possible for normal use.

    最近公司里面进行UOS的兼容性验证,但是系统总是会提示: Your system is not activated. Please activate as soon as possible for n ...

  7. echarts设置单位的偏移

    echarts 可以设置的echarts单位的偏移位置吗? 之前是知道echarts的X和Y是可以设置单位的. 但是设置单位的位置一直不好调整. 现在有时间,我们会回答一下上面标题的问题? echar ...

  8. 【发现一个小问题】坑爹的官方日志库`golang.org/x/exp/slog`,凭啥不让我设置debug级别日志

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 一个代码使用了官方的日志库"golang.org ...

  9. openim支持十万超级大群

    钉钉:根据相关监管要求,新建普通群人数上限调整为500人,不支持群人数扩容. 企业微信:内部群聊人数最多支持2000人,群个数无上限.全员群人数最多支持10000人.企业微信用户创建的外部群人数最多支 ...

  10. Unity2019及Unity2020打包android的环境配置

    2019安卓打包只有gradle模式了,因为谷歌把adt删了 unity2019可以自定义gradle模板,国内请把repo地址改成阿里云的源 unity2019打apk配置 引擎版本:unity20 ...