基于AQS的前世今生,来学习并发工具类Semaphore。本文将从Semaphore的应用场景、源码原理解析来学习这个并发工具类。

1、 应用场景

  Semaphore用来控制同时访问某个特定资源的操作数量,或者同时执行某个指定操作的数量。还可以用来实现某种资源池限制,或者对容器施加边界。

1.1   当成锁使用

  控制同时访问某个特定资源的操作数量,代码如下:

public class SemaphoreLock {
public static void main(String[] args) {
//1、信号量为1时 相当于普通的锁 信号量大于1时 共享锁
Output o = new Output();
for (int i = 0; i < 5; i++) {
new Thread(() -> o.output()).start();
}
}
}
class Output {
Semaphore semaphore = new Semaphore(1); public void output() {
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + " start at " + System.currentTimeMillis());
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " stop at " + System.currentTimeMillis());
}catch(Exception e) {
e.printStackTrace();
}finally {
semaphore.release();
}
}
}

1.2   线程通信信号

  线程间通信,代码如下:

public class SemaphoreCommunication {
public static void main(String[] args) {
//2、线程间进行通信
Semaphore semaphore = new Semaphore(1);
new SendingThread(semaphore,"SendingThread");
new ReceivingThread(semaphore,"ReceivingThread");
}
}
class SendingThread extends Thread {
Semaphore semaphore;
String name; public SendingThread(Semaphore semaphore,String name) {
this.semaphore = semaphore;
this.name = name;
new Thread(this).start();
} public void run() {
try {
semaphore.acquire();
for (int i = 0; i < 5; i++) {
System.out.println(name + ":" + i);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
semaphore.release();
}
} class ReceivingThread extends Thread {
Semaphore semaphore;
String name; public ReceivingThread(Semaphore semaphore,String name) {
this.semaphore = semaphore;
this.name = name;
new Thread(this).start();
} public void run() {
try {
semaphore.acquire();
for (int i = 0; i < 5; i++) {
System.out.println(name + ":" + i);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
semaphore.release();
}
}

1.3   资源池限制

  对资源池进行资源限制,代码如下:

public class SemaphoreConnect {
public static void main(String[] args) throws Exception {
//3、模拟连接池数量限制
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 200; i++) {
executorService.submit(new Runnable() {
@Override
public void run() {
Connection.getInstance().connect();
}
});
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.DAYS);
}
}
class Connection {
private static Connection instance = new Connection();
private Semaphore semaphores = new Semaphore(10,true);
private int connections = 0; private Connection() {
} public static Connection getInstance() {
return instance;
} public void connect() {
try {
semaphores.acquire();
doConnect();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
semaphores.release();
}
} private void doConnect() {
synchronized (this) {
connections ++;
System.out.println("current get connections is : " + connections);
} try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} synchronized (this) {
connections --;
System.out.println("after release current connections is : " + connections);
}
}
}

1.4  容器边界限制

  对容器进行边界限制,代码如下:

public class SemaphoreBoundedList {
public static void main(String[] args) {
//4、容器边界限制
final BoundedList ba = new BoundedList(5);
Runnable runnable1 = new Runnable() {
public void run() {
try {
ba.add("John");
ba.add("Martin");
ba.add("Adam");
ba.add("Prince");
ba.add("Tod");
System.out.println("Available Permits : " + ba.getSemaphore().availablePermits());
ba.add("Tony");
System.out.println("Final list: " + ba.getArrayList());
}catch (InterruptedException ie) {
Thread.interrupted();
}
}
};
Runnable runnable2 = new Runnable() {
public void run() {
try {
System.out.println("Before removing elements: "+ ba.getArrayList());
Thread.sleep(5000);
ba.remove("Martin");
ba.remove("Adam");
}catch (InterruptedException ie) {
Thread.interrupted();
}
}
};
Thread thread1 = new Thread(runnable1);
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
}
}
class BoundedList<T> {
private final Semaphore semaphore;
private List arrayList; BoundedList(int limit) {
this.arrayList = Collections.synchronizedList(new ArrayList());
this.semaphore = new Semaphore(limit);
} public boolean add(T t) throws InterruptedException {
boolean added = false;
semaphore.acquire();
try {
added = arrayList.add(t);
return added;
} finally {
if (!added)
semaphore.release();
} } public boolean remove(T t) {
boolean wasRemoved = arrayList.remove(t);
if (wasRemoved)
semaphore.release();
return wasRemoved;
} public void remove(int index) {
arrayList.remove(index);
semaphore.release();
} public List getArrayList() {
return arrayList;
} public Semaphore getSemaphore() {
return semaphore;
}
}

2、 源码原理解析

2.1 获取信号

  获取信号的方法如下:

public void acquire() throws InterruptedException {
sync.acquireSharedInterruptibly(1);//共享式获取AQS的同步状态
}

  调用的是AQS的acquireSharedInterruptibly方法:

public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted())//线程中断 说明信号量对线程中断敏感
throw new InterruptedException();
if (tryAcquireShared(arg) < 0) //获取信号量失败 线程进入同步队列自旋等待
doAcquireSharedInterruptibly(arg);
}

  其中tryAcquireShared依赖的是Sync的实现,Sync提供了公平和非公平式的方式,先看非公平式。

protected int tryAcquireShared(int acquires) {
return nonfairTryAcquireShared(acquires);
}
final int nonfairTryAcquireShared(int acquires) {
for (;;) {
int available = getState();//同步状态 当前的信号量许可数
int remaining = available - acquires;//减去释放的信号量 剩余信号量许可数
if (remaining < 0 ||//剩余信号量小于0 直接返回remaining 不做CAS
compareAndSetState(available, remaining))//CAS更新
return remaining;
}
}

  再看下公平式的。

protected int tryAcquireShared(int acquires) {
for (;;) {
if (hasQueuedPredecessors())//判断同步队列如果存在前置节点 获取信号量失败 其他和非公平式是一致的
return -1;
int available = getState();
int remaining = available - acquires;
if (remaining < 0 ||
compareAndSetState(available, remaining))
return remaining;
}
}

  最后来看下,如果未获取到信号量的处理方法doAcquireSharedInterruptibly。

private void doAcquireSharedInterruptibly(int arg)
throws InterruptedException {
final Node node = addWaiter(Node.SHARED);//线程进入同步队列
boolean failed = true;
try {
for (;;) {//自旋
final Node p = node.predecessor();
if (p == head) {//当前节点的前置节点是AQS的头节点 即自己是AQS同步队列的第一个节点
int r = tryAcquireShared(arg); //再去获取信号量
if (r >= 0) {//获取成功
setHeadAndPropagate(node, r);//退出自旋
p.next = null; // help GC
failed = false;
return;
}
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
throw new InterruptedException();
}
} finally {
if (failed)
cancelAcquire(node); //获取失败 就取消获取
}
}

2.2 释放信号

  释放信号的方法如下:

public void release() {
sync.releaseShared(1);
}

  调用的是AQS的releaseShared方法:

public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {//释放信号量
doReleaseShared();//唤醒后续的线程节点
return true;
}
return false;
}

  tryReleaseShared交由子类Sync实现,代码如下:

protected final boolean tryReleaseShared(int releases) {
for (;;) {
int current = getState();//当前信号量许可数
int next = current + releases; //当前信号量许可数+释放的信号量许可数
if (next < current) // overflow 这个分支我看着永远走不进来呢
throw new Error("Maximum permit count exceeded");
if (compareAndSetState(current, next))//CAS更新当前信号量许可数
return true;
}
}

  释放成功后,则继续调用doReleaseShared,唤醒后续线程节点可以来争取信号量了。

private void doReleaseShared() {
for (;;) {
Node h = head; //头节点
if (h != null && h != tail) {//同步队列中存在线程等待
int ws = h.waitStatus; //头节点线程状态
if (ws == Node.SIGNAL) {//头节点线程状态为SIGNAL 唤醒后续线程节点
if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
continue; // loop to recheck cases
unparkSuccessor(h); //唤醒下个节点
}
else if (ws == 0 &&
!compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
continue; // loop on failed CAS
}
if (h == head) // loop if head changed
break;
}
}

  总结:Semaphore使用AQS同步状态来保存信号量的当前计数。它里面定义的acquireSharedInterruptibly方法会减少计数,当计数为非正值时阻塞线程,releaseShared方法会增加计数,在计数不超过信号量限制时要解除线程的阻塞。

参考资料:

https://github.com/lingjiango/ConcurrentProgramPractice

https://www.caveofprogramming.com/java-multithreading/java-multithreading-semaphores-part-12.html

https://java2blog.com/java-semaphore-example/

http://tutorials.jenkov.com/java-util-concurrent/semaphore.html

Java并发编程-Semaphore的更多相关文章

  1. Java并发编程Semaphore

    信号量 信号量类Semaphore,用来保护对唯一共享资源的访问.一个简单的打印队列,并发任务进行打印,加入信号量同时之能有一个线程进行打印任务 . import java.util.concurre ...

  2. 【Java并发编程实战】-----“J.U.C”:Semaphore

    信号量Semaphore是一个控制访问多个共享资源的计数器,它本质上是一个"共享锁". Java并发提供了两种加锁模式:共享锁和独占锁.前面LZ介绍的ReentrantLock就是 ...

  3. Java并发编程:CountDownLatch、CyclicBarrier和Semaphore

    Java并发编程:CountDownLatch.CyclicBarrier和Semaphore 在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch ...

  4. Java并发编程的4个同步辅助类(CountDownLatch、CyclicBarrier、Semaphore、Phaser)

    我在<JDK1.5引入的concurrent包>中,曾经介绍过CountDownLatch.CyclicBarrier两个类,还给出了CountDownLatch的演示案例.这里再系统总结 ...

  5. Java并发编程:CountDownLatch、CyclicBarrier和Semaphore (总结)

    下面对上面说的三个辅助类进行一个总结: 1)CountDownLatch和CyclicBarrier都能够实现线程之间的等待,只不过它们侧重点不同: CountDownLatch一般用于某个线程A等待 ...

  6. 14、Java并发编程:CountDownLatch、CyclicBarrier和Semaphore

    Java并发编程:CountDownLatch.CyclicBarrier和Semaphore 在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch ...

  7. 【转】Java并发编程:CountDownLatch、CyclicBarrier和Semaphore

    Java并发编程:CountDownLatch.CyclicBarrier和Semaphore   Java并发编程:CountDownLatch.CyclicBarrier和Semaphore 在j ...

  8. java并发编程系列原理篇--JDK中的通信工具类Semaphore

    前言 java多线程之间进行通信时,JDK主要提供了以下几种通信工具类.主要有Semaphore.CountDownLatch.CyclicBarrier.exchanger.Phaser这几个通讯类 ...

  9. Java并发编程基础三板斧之Semaphore

    引言 最近可以进行个税申报了,还没有申报的同学可以赶紧去试试哦.不过我反正是从上午到下午一直都没有成功的进行申报,一进行申报 就返回"当前访问人数过多,请稍后再试".为什么有些人就 ...

随机推荐

  1. 从NoSQL到NewSQL,谈交易型分布式数据库建设要点

    在上一篇文章<从架构特点到功能缺陷,重新认识分析型分布式数据库>中,我们完成了对不同"分布式数据库"的横向分析,本文Ivan将讲述拆解的第二部分,会结合NoSQL与Ne ...

  2. Gephi安装过程中出现错误:can’t find java 1.8 or higher

    Gephi具体的安装过程我就不多说了,一直点击下一步就OK了,我想说的是出现如下图这种或者类似的错误怎么解决. 在百度的过程中发现很多的博文等等出现这个错误的解决方法都是安装对应版本的JDK啊,配置对 ...

  3. nginx安装配置并布置网站

    之前做的网站都是用的apache,关于apache和Nginx的区别也不说了,百度上也都有,而且apche和nginx可以共存,这个之后再说. 首先安装nginx,我用的云主机,直接用yum安装 #y ...

  4. git知识整理

    概述 工作中使用git进行代码托管,一开始只知道git add commit,之后了解了git-flow插件,觉得超牛逼,一键生成feature分支,再后来听说原生git命令更好用,于是又去学了原生g ...

  5. JSTL 和 EL

    EL表达式   Expression Language 语法${作用域中的值} 使用EL表达式时,需要在page标签中写上isELIgnored="false",否则EL表达式不生 ...

  6. Linux下安装、启动、停止mongodb

    1.下载完安装包,并解压 tgz(以下演示的是 64 位 Linux上的安装) curl .tgz # 下载 tar .tgz # 解压 mv mongodb/ /usr/local/mongodb ...

  7. 1.Git起步-Git的三种状态以及三种工作区域、CVCS与DVCS的区别、Git基本工作流程

    1.Git基础 版本控制系统是一种用于记录一个或多个文件内容变化,以便将来查阅恢复特定版本修订情况的系统. Git是一种分布式版本控制系统(Distributed Version Control Sy ...

  8. app测试之专项测试

    专项测试包含很多东西,安装.升级.卸载.性能.安全.网络.随机等等,这些都属于专项测试 一个app的正常到用户手里使用,功能是最基础的测试,专项测试测试主要的. 下面介绍一些常用的专项测试: 1.多任 ...

  9. 【翻译】浏览器渲染Rendering那些事:repaint、reflow/relayout、restyle

    原文链接:http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/ 有没有被标题中的5个“R”吓到?今天,我们来讨论一下浏览器的渲 ...

  10. 手写spring(简易版)

    本文版权归 远方的风lyh和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作,如有错误之处忘不吝批评指正! 理解Spring本质: 相信之前在使用spring的时候大家都配置web.x ...