semaphore

Java 信号量 Semaphore

邮差的信 提交于 2020-01-27 21:58:49
public static void main(String[] args) { // 线程池 ExecutorService exec = Executors.newCachedThreadPool(); // 只能5个线程同时访问 final Semaphore semp = new Semaphore(5); // 模拟20个客户端访问 for (int index = 0; index < 20; index++) { final int NO = index; Runnable run = new Runnable() { public void run() { try { // 获取许可 semp.acquire(); System.out.println("Accessing: " + NO); Thread.sleep((long) (Math.random() * 10000)); // 访问完后,释放 semp.release(); System.out.println("-----------------"+semp.availablePermits()); } catch (InterruptedException e) { e.printStackTrace(); } } }; exec.execute(run); } // 退出线程池 exec

Using semaphore to control number of threads

北慕城南 提交于 2020-01-25 18:39:05
问题 How can I make use of Semaphore class in order to control number of threads that have an access to an object? 回答1: Initialize the Semaphore with the max number of alowed threds, reduce the semaphore counter by one if a thread enters the restricted area increse the semaphore counter by one if the thred leaves the restricted area 回答2: One more good thing about using semaphore to control access to resouce is you can resize your semaphore at runtime. For eg you can have some use case where you

Semaphore

大兔子大兔子 提交于 2020-01-25 00:53:57
Semaphore 信号量 semaphore有两种方式,一种是公平的方式,一种是非公平的方式。 public Semaphore(int permits, boolean fair) { sync = fair ? new FairSync(permits) : new NonfairSync(permits); } 非公平只体现在加锁的同时看是否有空闲的资源,如果有,就取,如果没有再排队。 公平,就是直接去排队。 acquire: 信号量用的还是aqs中的sync,构造方法中的permits是信号量的数量。 同时被设置到了aqs中的state中。 而acquire的方法实现在了Semaphore中 1. 如果有前驱节点,也就是前面有人在等,那就失败 2. 如果没有前驱节点,取当前sync的state,根据state-acquire算出remaining,如果remaining<0 或者修改成功就返回 2.1 如果remaining<0返回后,会生成节点加入aqs的队列中 2.1.1 如果是head,那么再取一次锁,如果取到了锁,则通知下一个 2.1.2 否则,park线程 release: 1. 给state增加当前的信号量数量 2. 通知sync队列中的下一个,waitStatus到底是0还是-3??怎么通知下一个?? 但是有个疑问,为什么next会<current呢?

FIFO semaphore test

旧时模样 提交于 2020-01-24 15:55:09
问题 I have implemented FIFO semaphores but now I need a way to test/prove that they are working properly. A simple test would be to create some threads that try to wait on a semaphore and then print a message with a number and if the numbers are in order it should be FIFO, but this is not good enough to prove it because that order could have occurred by chance. Thus, I need a better way of testing it. If necessary locks or condition variables can be used too. Thanks 回答1: What you describe with

sem_timedwait not supported properly on RedHat Enterprise Linux 5.3 onwards?

▼魔方 西西 提交于 2020-01-24 04:21:25
问题 We're seeing odd behaviour on RedHat Enterprise Linux systems with pthreads sem_timedwait. It's only occurring with versions 5.3 onwards. When we create the semaphore on a background thread with sem_init, no error is returned. When we do sem_timedwait, we get an immediate return with errno = 38 (ENOSYS) indicating it's not supported. If we do the same thing on the main thread, it works as expected and we get no error from sem_timedwait. We don't see it on RHEL 5.2 or before. We've tried

并发编程学习笔记之十三并发工具Semaphore信号量

匆匆过客 提交于 2020-01-24 02:31:11
一、Semaphore简介 Semaphore被称之为信号量,可以用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源。常常用来做流量控制,特别公共资源有限的应用场景,比如池化资源:数据库连接池,对象池,线程池。来举个更接地气的例子:一个临时停车场只有10个车位,一次来了10辆车,正好将10个车位占满,此时又来了一辆车,只能等其他车离开之后才能找到车位停车。 二、简单应用 下面来看Semaphore一个简单的应用示例:Semaphore相当于车位,此时5辆车依次进入停车产。 package cn.com.threadtx.cn.com; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** *Semaphore信号量示例 */ public class SemaphoreTest { public static void main(String[] args) { //初始化线程池 ExecutorService service = Executors.newCachedThreadPool(); //创建信号量 final Semaphore sp =

Regarding the usage of SemaphoreSlim with Async/Await

[亡魂溺海] 提交于 2020-01-23 06:48:13
问题 I am not an advanced developer. I'm just trying to get a hold on the task library and just googling. I've never used the class SemaphoreSlim so I would like to know what it does. Here I present code where SemaphoreSlim is used with async & await but which I do not understand. Could someone help me to understand the code below. 1st set of code await WorkerMainAsync(); async Task WorkerMainAsync() { SemaphoreSlim ss = new SemaphoreSlim(10); while (true) { await ss.WaitAsync(); // you should

Regarding the usage of SemaphoreSlim with Async/Await

左心房为你撑大大i 提交于 2020-01-23 06:47:32
问题 I am not an advanced developer. I'm just trying to get a hold on the task library and just googling. I've never used the class SemaphoreSlim so I would like to know what it does. Here I present code where SemaphoreSlim is used with async & await but which I do not understand. Could someone help me to understand the code below. 1st set of code await WorkerMainAsync(); async Task WorkerMainAsync() { SemaphoreSlim ss = new SemaphoreSlim(10); while (true) { await ss.WaitAsync(); // you should

Java并发--控制并发线程数的Semaphore

浪子不回头ぞ 提交于 2020-01-21 19:17:41
控制并发线程数的Semaphore Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源。 Semaphore比作是控制流量的红绿灯。比如××马路要限制流量,只允许同时有一百辆车在这条路上行使,其他的都必须在路口等待,所以前一百辆车会看到绿灯,可以开进这条马路,后面的车会看到红灯,不能驶入××马路,但是如果前一百辆中有5辆车已经离开了××马路,那么后面就允许有5辆车驶入马路,这个例子里说的车就是线程,驶入马路就表示线程在执行,离开马路就表示线程执行完成,看见红灯就表示线程被阻塞,不能执行。 应用场景: Semaphore可以用于做流量控制,特别是公用资源有限的应用场景,比如数据库连接。假如有一个需求,要读取几万个文件的数据,因为都是IO密集型任务,我们可以启动几十个线程并发地读取,但是如果读到内存后,还需要存储到数据库中,而数据库的连接数只有10个,这时我们必须控制只有10个线程同时获取数据库连接保存数据,否则会报错无法获取数据库连接。这个时候,就可以使用Semaphore来做流量控制,如下所示。 package pers . zhang . part6 ; import java . util . concurrent . ExecutorService ; import java . util . concurrent

限流的介绍以及限流的代码实现

夙愿已清 提交于 2020-01-21 05:27:11
限流的介绍以及限流的实现 什么是限流 常用的限流方式 信号量 令牌桶 什么是限流 限流就是对某一段时间窗口内的请求数进行控制,保持系统的可用性和稳定性。 防止因为流量的暴增而导致系统奔溃。 很多平台都有限流,例如我们调用第三方的api的时候,对方都会限制每天最多只能调用几次之类的。 常用的限流方式 1)信号量 2)令牌桶 信号量 Semaphore是一个计数信号量。常用于限制获取某资源的线程数量, 可用java的 Semaphore 实现。 原理就是先定义总共有多少个信号量,然后每个线程进来拿到几个信号量才可以成功访问,这样就允许了同时有多少个线程访问。 例如,定义了6个信号量,每个线程进来拿2个,那么同时允许3个线程成功访问。 通过acquire()方法获取许可,该方法会阻塞,直到获取许可为止。 通过release()方法释放许可。 代码实现如下 @RestController @RequestMapping ( "/semaphore" ) public class SemaphoreController { //初始化信号量=5,true-公平信号量FIFO。 private Semaphore semaphore = new Semaphore ( 5 , true ) ; //该方法是阻塞的,直到获取许可为止 @RequestMapping ( "/acquire/