Java 并发编程学习笔记(3) ----Semaphore-tryAcquire()的使用

2024-04-20 17:38

本文主要是介绍Java 并发编程学习笔记(3) ----Semaphore-tryAcquire()的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

tryAcquire()的使用

参数使用

当前时刻tryAcquire(int permits)Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.尝试去从这个信号量获取指定数量的在调用时都是可用的许可 。如果不使用permits 参数,tryAcquire()表示获取一个许可。指定时间tryAcquire(int permits, long timeout, TimeUnit unit)Acquires the given number of permits from this semaphore,if all become available within the given waiting time and the current thread has not been interrupted.在指定的时间内尝试去从这个信号量获取指定数量的许可 ,同时这段时间内,这个线程没有被中断。如果不使用permits 参数,tryAcquire(long timeout, TimeUnit unit)表示获取一个许可。  

代码


package com.lhc.concurrent.semaphore;import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;public class TryAcquireService {private Semaphore semaphore = new Semaphore(8);public void doSomething() {try {/*** tryAcquire(int permits)* Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.* 尝试去从这个信号量获取指定数量的在调用时都是可用的许可 。* 如果不使用permits 参数,tryAcquire()表示获取一个许可。*/if (semaphore.tryAcquire(2)) {System.out.println(Thread.currentThread().getName() + "获得锁,时间:" + System.currentTimeMillis());Thread.sleep(100);semaphore.release(2);}else {System.out.println(Thread.currentThread().getName() + "没有获得锁,时间:" + System.currentTimeMillis());}}catch (InterruptedException e){e.printStackTrace();}}public void doThing() {try {/*** tryAcquire(int permits, long timeout, TimeUnit unit)* Acquires the given number of permits from this semaphore,* if all become available within the given waiting time and the current thread has not been interrupted.* 在指定的时间内尝试去从这个信号量获取指定数量的许可 ,同时这段时间内,这个线程没有被中断。* 如果不使用permits 参数,tryAcquire(long timeout, TimeUnit unit)表示获取一个许可。*/if (semaphore.tryAcquire(2, 1, TimeUnit.SECONDS)) {System.out.println(Thread.currentThread().getName() + "获得锁,时间:" + System.currentTimeMillis());Thread.sleep(1000);System.out.println(Thread.currentThread().getName() + "释放锁,时间:" + System.currentTimeMillis());semaphore.release(2);}else {System.out.println(Thread.currentThread().getName() + "没有获得锁,时间:" + System.currentTimeMillis());}}catch (InterruptedException e){e.printStackTrace();}}
}

测试类


package com.lhc.concurrent.semaphore;public class TryAcquireThread extends Thread{private TryAcquireService tryAcquireService;public TryAcquireThread(TryAcquireService tryAcquireService, String name) {super();this.tryAcquireService = tryAcquireService;this.setName(name);}public static void main(String[] args){TryAcquireService tryAcquireService = new TryAcquireService();for (int i = 0; i < 10; i++) {TryAcquireThread tryAcquireThread = new TryAcquireThread(tryAcquireService, "线程" + i);tryAcquireThread.start();}}@Overridepublic void run() {//tryAcquireService.doSomething();tryAcquireService.doThing();}
}

测试结果

获取当前时刻

线程0获得锁,时间:1555145916044
线程4没有获得锁,时间:1555145916044
线程2获得锁,时间:1555145916044
线程1获得锁,时间:1555145916044
线程6没有获得锁,时间:1555145916044
线程5没有获得锁,时间:1555145916044
线程3获得锁,时间:1555145916044
线程7没有获得锁,时间:1555145916044
线程8没有获得锁,时间:1555145916044
线程9没有获得锁,时间:1555145916044

获取指定时间内

线程9获得锁,时间:1555146046722
线程7获得锁,时间:1555146046722
线程1获得锁,时间:1555146046722
线程6获得锁,时间:1555146046722
线程6释放锁,时间:1555146047722
线程9释放锁,时间:1555146047722
线程5获得锁,时间:1555146047722
线程4获得锁,时间:1555146047722
线程3没有获得锁,时间:1555146047722
线程1释放锁,时间:1555146047722
线程7释放锁,时间:1555146047722
线程0没有获得锁,时间:1555146047722
线程8没有获得锁,时间:1555146047722
线程2没有获得锁,时间:1555146047722
线程5释放锁,时间:1555146048723
线程4释放锁,时间:1555146048723

这篇关于Java 并发编程学习笔记(3) ----Semaphore-tryAcquire()的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/920914

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依