【信号量】:
- 用于控制对某资源访问的同一时间的并发量。
【如何获取】:
- semaphore.tryAcquire(),尝试获取,不阻塞
- semaphore.acquire(),没信号量可用时,将进行阻塞等
【如何释放】:
- semaphore.release();
- 线程抛出各种异常,都别忘了在finally中释放信号量;
- 如果释放的比获取的信号量还多,例如获取了2个,释放了5次,那么当前信号量就动态的增加为5了,要注意。
【动态增加】:
- 多释放几次,就可以达到信号量动态增加的效果了
【动态减小】:
- 信号量本来有这个api的,不过是protected方法,所以我们需要显式继续Semaphore,并重新实现该api,见ResizeableSemaphore类中的reducePermits(int reduction);
- 举例如下:(该表格有个假设前提,不存在多余的release而产生出新的信号量,即release次数<=acquire次数)
当前信号量A (A=A1+A2) |
占用信号量A1 | 可用信号量A2 | 重新设置信号量B (B=B1+B2) |
当前可用的信号量B1 | 当前待释放的量B2 |
5 | 3 | 2 | 3 | 0 | 0 |
5 | 3 | 2 | 1 | 0 | -2 |
5 | 3 | 2 | 9 | 6 | 0 |
package com.jd.las.basic.service; import java.util.concurrent.Semaphore; /** * * Title: 动态信号量<br> * * Description: <br> * * Company: <a href=www.jd.com>京东</a><br> * * @author <a href=mailto:longzhun@jd.com>龙准</a> * * @date 2015年5月26日 下午7:29:42 */ public class JdSemaphore { /** * semaphore starts at 0 capacity; must be set by setMaxPermits before use */ private final ResizeableSemaphore semaphore = new ResizeableSemaphore(); /** * how many permits are allowed as governed by this semaphore. * Access must be synchronized on this object. */ private int maxPermits = 0; /** * New instances should be configured with setMaxPermits(). */ public JdSemaphore() { // no op } /* * Must be synchronized because the underlying int is not thread safe */ /** * Set the max number of permits. Must be greater than zero. * * Note that if there are more than the new max number of permits currently * outstanding, any currently blocking threads or any new threads that start * to block after the call will wait until enough permits have been released to * have the number of outstanding permits fall below the new maximum. In * other words, it does what you probably think it should. * * @param newMax */ public synchronized void setMaxPermits(int newMax) { if (newMax < 1) { throw new IllegalArgumentException("Semaphore size must be at least 1," + " was " + newMax); } int delta = newMax - this.maxPermits; if (delta == 0) { return; } else if (delta > 0) { // new max is higher, so release that many permits this.semaphore.release(delta); } else { delta *= -1; // delta < 0. // reducePermits needs a positive #, though. this.semaphore.reducePermits(delta); } this.maxPermits = newMax; } /** * Release a permit back to the semaphore. Make sure not to double-release. * */ public void release() { this.semaphore.release(); } /** * Get a permit, blocking if necessary. * * @throws InterruptedException * if interrupted while waiting for a permit */ public void acquire() throws InterruptedException { this.semaphore.acquire(); } /** * A trivial subclass of <code>Semaphore</code> that exposes the reducePermits * call to the parent class. Doug Lea says it's ok... * http://osdir.com/ml/java.jsr.166-concurrency/2003-10/msg00042.html */ private static final class ResizeableSemaphore extends Semaphore { /** * */ private static final long serialVersionUID = 1L; /** * Create a new semaphore with 0 permits. */ ResizeableSemaphore() { super(0); } @Override protected void reducePermits(int reduction) { super.reducePermits(reduction); } } }
这里可参考:http://blog.teamlazerbeez.com/2009/04/20/javas-semaphore-resizing/
相关推荐
使用信号量(Semaphore)实现线程的同步
Semaphore是计数信号量。Semaphore管理一系列许可证。每个acquire方法阻塞,直到有一个许可证可以获得然后拿走一个许可证;每个release方法增加一个许可证,这可能会释放一个阻塞的acquire方法。然而,其实并没有...
信号量(Semaphore)是一种实现任务间通信的机制,可以实现任务之间同步或临界资源的互斥访问 (临界资源指同一时刻只能有有限个访问),常用于协助一组相互竞争的任务来访问临界资源。运行机制可以理解为:信号量是...
异步信号量 基于 Java 的并发信号量的计数信号量。 安装 通过 npm 安装模块: npm install async-semaphore 快速示例 // fairness false var Semaphore = require ( 'async-semaphore' ) ; var semaphore = new ...
信号量在go语言上实现几种信号量模式。例子基本信号量BasicSemaphore是一个管理固定数量的并发任务的信号灯。 func TestSemaphore ( t * testing. T ) { permit := 3 sem := semaphore . NewSemaphore ( permit ) ...
信号量(Semaphore)哲学家进餐问题(the dining philosophers problem)---------------------------程序
从0.8.6版本开始,bordeaux-threads有其自己的内置信号量,因此您绝对应该使用它代替bt-semaphore。 安装 bt-semaphore可通过。 如果您好奇,还可以克隆Git存储库: cd ~/quicklisp/local-projects git clone ...
资源包含了vxworks多任务(task和semaphore信号量及事件)的相关编程例子,通过创建多任务的简单例子,帮助大家了解taskSpawn函数、信号量semaphore、以及事件event的使用方法。
难堪的一次面试: 信号量Semaphore了解过吗?没有
介绍临界资源的访问机制,有Semaphore,signal,mutex等
多线程同时运行,能提高程序的运行效率,但是并非线程越多越好,而semaphore信号量可以通过内置计数器来控制同时运行线程的数量,启动线程(消耗信号量)内置计数器会自动减一,线程结束(释放信号量)内置计数器会自动...
信号 基于CAS的可快速调整大小的golang信号量允许加权获取/释放; 支持通过上下文取消; 创建后允许更改信号量限制; 比基于通道的信号量更快。用法发起import "github....
Football_using_semaphore 使用信号量和共享内存模拟足球比赛
信号 信号量是一个非常简单的Android应用程序,可以在屏幕上显示信号量。 只是一个信号量,仅此而已。 您可以点击屏幕上的,并且指示灯会顺序切换。
Delphi多线程之Semaphore_(信号对象).pdf
主要介绍了Java并发编程Semaphore计数信号量详解,具有一定参考价值,需要的朋友可以了解下。
1、信号量:就是一种可用来控制访问资源的数量的标识,设定了一个信 2、信号量主要有3个函数,分别是: 3、那么就开头提的问题,我们用代码来解决
关于Android变成中的信号量semaphore,在使用过程中我们往往搞不清楚其信号量的申请和使用。这里主要针对信号量初始值为零的情况做介绍,其他情况不做讨论。初始值为零的情况较少使用,部分代码中的这个情况往往让...
释放信号量_tx_semaphore_put 1,如果tx_semaphore_suspension_list挂起队列为空,那么直接把tx_semaphore_count计数器加一 2,如果tx_semaphore_suspension_list挂起队列不为空,那么tx_semaphore_suspension_list...
主要介绍了Java中Semaphore(信号量)的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧