Concurrent - Semaphore - acquire(int permits)

2023-10-11 20:20

本文主要是介绍Concurrent - Semaphore - acquire(int permits),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原创转载请注明出处:http://agilestyle.iteye.com/blog/2342898

 

有参方法acquire(int permits)表示每调用1次此方法,就使用Semaphore中的x个permits



Service.java

package org.fool.java.concurrent.semaphore.acquire;import java.util.concurrent.Semaphore;public class Service {private Semaphore semaphore = new Semaphore(10);public void testMethod() {try {semaphore.acquire(2);System.out.println(Thread.currentThread().getName() + " begin timer=" + System.currentTimeMillis());int sleepValue = 5000;System.out.println(Thread.currentThread().getName() + " stop " + (sleepValue / 1000) + " sec");Thread.sleep(sleepValue);System.out.println(Thread.currentThread().getName() + " end timer=" + System.currentTimeMillis());semaphore.release(2);} catch (InterruptedException e) {e.printStackTrace();}}
}

Note:

private Semaphore semaphore = new Semaphore(10);

定义10个permits,每次执行semaphore.acquire(2);消耗掉2个,所以10/2=5,说明同一时间只允许5个线程执行acquire()和release()之间的代码。 

ThreadA.java

package org.fool.java.concurrent.semaphore.acquire;public class ThreadA implements Runnable {private Service service;public ThreadA(Service service) {this.service = service;}@Overridepublic void run() {service.testMethod();}
}

SemaphoreTest.java

package org.fool.java.concurrent.semaphore.acquire;public class SemaphoreTest {public static void main(String[] args) throws InterruptedException {Service service = new Service();for (int i = 0; i < 10; i++) {new Thread(new ThreadA(service)).start();}}
}

Run


 

如果多次调用Semaphore的release()或release(int)方法时,还可以动态增加permits的个数。

package org.fool.java.concurrent.semaphore.acquire;import java.util.concurrent.Semaphore;public class SemaphoreTest2 {public static void main(String[] args) {try {Semaphore semaphore = new Semaphore(5);semaphore.acquire();semaphore.acquire();semaphore.acquire();semaphore.acquire();semaphore.acquire();System.out.println(semaphore.availablePermits());   // outputs 0semaphore.release();semaphore.release();semaphore.release();semaphore.release();semaphore.release();semaphore.release();System.out.println(semaphore.availablePermits());   // outputs 6semaphore.release(4);System.out.println(semaphore.availablePermits());   // outputs 10} catch (InterruptedException e) {e.printStackTrace();}}
}

Note:

Semaphore semaphore = new Semaphore(5);

 定义的5并不是最终的permits数量,仅仅是初始的状态值。

 

 

 

 

这篇关于Concurrent - Semaphore - acquire(int permits)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python 中考虑 concurrent.futures 实现真正的并行计算

Python 中考虑 concurrent.futures 实现真正的并行计算 思考,如何将代码所要执行的计算任务划分成多个独立的部分并在各自的核心上面平行地运行。 Python 的全局解释器锁(global interpreter lock,GIL)导致没办法用线程来实现真正的并行​,所以先把这种方案排除掉。另一种常见的方案,是把那些对性能要求比较高的(performance-critica

【C语言】---- 基本数据类型(char、int、float)

1 基本数据类型 C语言中的基本数据类型包括整型、浮点型和字符型,每种类型都有不同的存储大小和表示范围。以下是它们的常见表示形式和特点: 1.1 字符型 char类型用于表示单个字符,通常用于表示文本数据。char类型也被用来存储字符,但也可以用来存储较小的整数。在C语言中,char类型的大小一般为1字节(8位)。char类型可以是有符号的或无符号的,这取决于编译器和平台的实现。 1.2

如何简便的将List<Integer>转换成int[]?

使用Java 8的流(Streams)  ArrayList<Integer> list = new ArrayList<>();int[] intArray = list.stream().mapToInt(Integer::intValue).toArray();  若是maven项目可使用Apache Commons Lang库 <dependency> <groupId>

Syntax error on token int, VariableDeclaratorId expected after this token

Syntax error on token "int", VariableDeclaratorId expected after this token,看图,   <item name=" " type="id"/>; 这个的name没有,看图 删掉这行就行了,R.java就不会报错了!!!!!!!!!!!

java 多线程 CountDownLatch、CyclicBarrier、Semaphore

在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法。   以下是本文目录大纲:   一.CountDownLatch用法   二.CyclicBarrier用法   三.Semaphore用法   若有不正之处请多多谅解,并欢迎批评指正。

MySQL数据类型 int(M)中M含义

int(M)我们先来拆分,int是代表整型数据那么中间的M应该是代表多少位了,后来查mysql手册也得知了我的理解是正确的,下面我来举例说明。 MySQL 数据类型中的 integer types 有点奇怪。你可能会见到诸如:int(3)、int(4)、int(8) 之类的 int 数据类型。刚接触 MySQL 的时候,我还以为 int(3) 占用的存储空间比 int(4) 要小, int(4)

int数组和String字符串如何相互转化?

使用Java 8的Stream API 例如 int[] nums={1,2,3}  转化为 “123” String str = Arrays.stream(nums)// 将int转换为String .mapToObj(String::valueOf)// 使用分隔符连接 若需要,则Collectors.joining(",").collect(Collectors.joining());

java多线程学习--java.util.concurrent

题记:util和concurrent 包是后续重点先看的和学习的模块 原文地址:http://www.cnblogs.com/sunhan/p/3817806.html   CountDownLatch,api 文档:http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html

golang string转int,int转string

这个写业务代码的时候非常常用。 1、常用提出来 string转成int:int, err := strconv.Atoi(string)string转成int64:int64, err := strconv.ParseInt(string, 10, 64)int转成string:string := strconv.Itoa(int)int64转成string:string :=

C语言 int uint16_t 踩坑记录

使用 uint16_t 存储 int的负数,有可能读出来是65535 ? 是的,如果你尝试使用 uint16_t 类型来存储一个负数 int 值,你可能会得到 65535。这是因为 uint16_t 是一个无符号的16位整数类型,它的取值范围是从 0 到 65535(即 0 到 2^16 - 1)。当将一个负数强制转换为 uint16_t 时,实际上会发生一种称为“类型提升”的过程,这个过程会将