JUC包下Semaphore学习笔记
在Java的并发包中,Semaphore类表示信号量。Semaphore内部主要通过AQS(AbstractQueuedSynchronizer)实现线程的管理。Semaphore有两个构造函数,参数permits表示许可数,它最后传递给了AQS的state值。线程在运行时首先获取许可,
如果成功,许可数就减1,线程运行,当线程运行结束就释放许可,许可数就加1。
如果许可数为0,则获取失败,线程位于AQS的等待队列中,它会被其它释放许可的线程唤醒。在创建Semaphore对象的时候还可以指定它的公平性。
一般常用非公平的信号量,非公平信号量是指在获取许可时先尝试获取许可,
而不必关心是否已有需要获取许可的线程位于等待队列中,如果获取失败,才会入列。
而公平的信号量在获取许可时首先要查看等待队列中是否已有线程,如果有则入列。
先看测试案例:
package com.cxy.cyclicBarrier; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit; /**
* Created by Administrator on 2017/4/10.
*/
public class CxyDemo {
private final static int threadCount = ; public static void main(String[] args) throws Exception { ExecutorService exec = Executors.newCachedThreadPool(); final Semaphore semaphore = new Semaphore(); for (int i = ; i < threadCount; i++) {
System.out.println(i+"----------------------");
final int threadNum = i;
exec.execute(() -> {
try {
if (semaphore.tryAcquire(, TimeUnit.MILLISECONDS)) { // 尝试获取一个许可
test(threadNum);
semaphore.release(); // 释放一个许可
}
} catch (Exception e) {
// log.error("exception" , e);
System.out.println(e);
}
});
}
exec.shutdown();
} private static void test(int threadNum) throws Exception {
// log.info("{}" , threadNum);
System.out.println("a"+threadNum);
Thread.sleep();
} }
执行结果:
源码分析:
构造方法:
public Semaphore(int permits) {
sync = new NonfairSync(permits);
} /**
* Creates a {@code Semaphore} with the given number of
* permits and the given fairness setting.
*
* @param permits the initial number of permits available.
* This value may be negative, in which case releases
* must occur before any acquires will be granted.
* @param fair {@code true} if this semaphore will guarantee
* first-in first-out granting of permits under contention,
* else {@code false}
*/
public Semaphore(int permits, boolean fair) {
sync = fair ? new FairSync(permits) : new NonfairSync(permits);
}
第一个构造方法:是允许的信号量
第二个,里面传入的boolean参数,采用的是公平锁还是分公平锁
tryAcquire源码
public boolean tryAcquire(long timeout, TimeUnit unit)
throws InterruptedException {
return sync.tryAcquireSharedNanos(, unit.toNanos(timeout));
} public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
return tryAcquireShared(arg) >= ||
doAcquireSharedNanos(arg, nanosTimeout);
} private boolean doAcquireSharedNanos(int arg, long nanosTimeout)
throws InterruptedException {
if (nanosTimeout <= 0L)
return false;
final long deadline = System.nanoTime() + nanosTimeout;
final Node node = addWaiter(Node.SHARED);
boolean failed = true;
try {
for (;;) {
final Node p = node.predecessor();
if (p == head) {
int r = tryAcquireShared(arg);
if (r >= ) {
setHeadAndPropagate(node, r);
p.next = null; // help GC
failed = false;
return true;
}
}
nanosTimeout = deadline - System.nanoTime();
if (nanosTimeout <= 0L)
return false;
if (shouldParkAfterFailedAcquire(p, node) &&
nanosTimeout > spinForTimeoutThreshold)
LockSupport.parkNanos(this, nanosTimeout);
if (Thread.interrupted())
throw new InterruptedException();
}
} finally {
if (failed)
cancelAcquire(node);
}
}
release源码
public void release() {
sync.releaseShared();
} public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {
doReleaseShared();
return true;
}
return false;
} private void doReleaseShared() {
/*
* Ensure that a release propagates, even if there are other
* in-progress acquires/releases. This proceeds in the usual
* way of trying to unparkSuccessor of head if it needs
* signal. But if it does not, status is set to PROPAGATE to
* ensure that upon release, propagation continues.
* Additionally, we must loop in case a new node is added
* while we are doing this. Also, unlike other uses of
* unparkSuccessor, we need to know if CAS to reset status
* fails, if so rechecking.
*/
for (;;) {
Node h = head;
if (h != null && h != tail) {
int ws = h.waitStatus;
if (ws == Node.SIGNAL) {
if (!compareAndSetWaitStatus(h, Node.SIGNAL, ))
continue; // loop to recheck cases
unparkSuccessor(h);
}
else if (ws == &&
!compareAndSetWaitStatus(h, , Node.PROPAGATE))
continue; // loop on failed CAS
}
if (h == head) // loop if head changed
break;
}
}
JUC包下Semaphore学习笔记的更多相关文章
- JUC包下CyclicBarrier学习笔记
CyclicBarrier,一个同步辅助类,在API中是这么介绍的: 它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这 ...
- JUC包下CountDownLatch学习笔记
CountDownLatch的作用是能使用多个线程进来之后,且线程任务执行完毕之后,才执行, 闭锁(Latch):一种同步方法,可以延迟线程的进度直到线程到达某个终点状态.通俗的讲就是,一个闭锁相当于 ...
- Linux下iptables学习笔记
Linux下iptables学习笔记 在Centos7版本之后,防火墙应用已经由从前的iptables转变为firewall这款应用了.但是,当今绝大多数的Linux版本(特别是企业中)还是使用的6. ...
- YOLO---Darknet下的学习笔记 V190319
YOLO---Darknet下的学习笔记 @WP 20190319 很久没有用YOlO算法了,今天又拿过来玩玩.折腾半天,才好运行通的,随手记一下: 一是,终端下的使用.二是,python接口的使用. ...
- YOLO---Darknet下的学习笔记
YOLO.V3-Darknet下的学习笔记 @wp20180927 [目录] 一. 安装Darknet(仅CPU下) 2 1.1在CPU下安装Darknet方式 2 1.2在GPU下安装Darknet ...
- JUC.Lock(锁机制)学习笔记[附详细源码解析]
锁机制学习笔记 目录: CAS的意义 锁的一些基本原理 ReentrantLock的相关代码结构 两个重要的状态 I.AQS的state(int类型,32位) II.Node的waitStatus 获 ...
- 20135202闫佳歆--week5 系统调用(下)--学习笔记
此为个人笔记存档 week 5 系统调用(下) 一.给MenuOS增加time和time-asm命令 这里老师示范的时候是已经做好的了: rm menu -rf 强制删除 git clone http ...
- juc包:使用 juc 包下的显式 Lock 实现线程间通信
一.前置知识 线程间通信三要素: 多线程+判断+操作+通知+资源类. 上面的五个要素,其他三个要素就是普通的多线程程序问题,那么通信就需要线程间的互相通知,往往伴随着何时通信的判断逻辑. 在 java ...
- 【Java多线程】JUC包下的工具类CountDownLatch、CyclicBarrier和Semaphore
前言 JUC中为了满足在并发编程中不同的需求,提供了几个工具类供我们使用,分别是CountDownLatch.CyclicBarrier和Semaphore,其原理都是使用了AQS来实现,下面分别进行 ...
随机推荐
- Screen - BOM对象
Screen 对象 Screen 对象包含有关客户端显示屏幕的信息. 注释:没有应用于 screen 对象的公开标准,不过所有浏览器都支持该对象. Screen 对象属性 属性 描述 availHei ...
- init方法返回值自动改写问题
[init方法返回值自动改写问题] 在ARC开启的情况下,以init开头的实例方法的返回值会被默认无视,返回类型会被编译器改写为类指针类型. 如一人类叫UIButton类,如果一个方法叫 (UILab ...
- 安装 Windows Service
1.打开 VS 命令行窗口 2. installutil /u service文件路径 (卸载原有服务) 3, installutil /i service 文件路径 (安装服务)
- selenium2 断言失败自动截图 (四)
一般web应用程序出错过后,会抛出异常.这个时候能截个图下来,当然是极好的. selenium自带了截图功能. //获取截图file File scrFile= ((TakesScreenshot)d ...
- 2-JRE System Libraty [eclipse-mars](unbound)
- Browser
浏览器中关于事件的那点事儿 作者: 顽Shi 发布时间: 2014-02-01 20:22 阅读: 7830 次 推荐: 25 原文链接 [收藏] 摘要:事件在Web前端领域有很重要 ...
- jquery 遮罩层指定位置
.css .datagrid-mask-msg { position: absolute; top: %; margin-top: -20px; padding: 12px 5px 10px 30px ...
- hdu 4768 Flyer (异或操作的应用)
2013年长春网络赛1010题 继巴斯博弈(30分钟)签到后,有一道必过题(一眼即有思路). 思路老早就有(40分钟):倒是直到3小时后才被A掉.期间各种换代码姿态! 共享思路: unlucky st ...
- php二维数组修改键名
最近遇到一个问题,是关于json数据提交的时候,总是报出[object object]的错误,查了晚上需要资料,大部分的说法是json数据格式不规范导致的错误.一般建议说将dataType类型注释掉. ...
- DataAnnotationsModelValidator-基于数据注解方式的model验证器
http://www.cnblogs.com/artech/archive/2012/04/10/how-mvc-works.html http://www.cnblogs.com/artech/ar ...