java限流(一): Semaphore
Before obtaining an item each thread must acquire a permit from the semaphore, guaranteeing that an item is available for use.
When the thread has finished with the item it is returned back to the pool and a permit is returned to the semaphore, allowing
another thread to acquire that item. Note that no synchronization lock is held when acquire() is called as that would prevent an
item from being returned to the pool. The semaphore encapsulates the synchronization needed to restrict access to the pool,
separately from any synchronization needed to maintain the consistency of the pool itself.
A semaphore initialized to one, and which is used such that it only has at most one permit available, can serve as a mutual
exclusion lock. This is more commonly known as a binary semaphore, because it only has two states: one permit available,
or zero permits available. When used in this way, the binary semaphore has the property (unlike many Lock implementations),
that the "lock" can be released by a thread other than the owner (as semaphores have no notion of ownership). This can be
useful in some specialized contexts, such as deadlock recovery.
场景:20个人需要买票,但只有2个窗口
问题:如何控制同一时间并发数为2 ?
public class SemaphoreDemo {
class SemaphoreRunnable implements Runnable{
private Semaphore semaphore;
private int user;
public SemaphoreRunnable(Semaphore semaphore, int user) {
this.semaphore = semaphore;
this.user = user;
}
@Override
public void run() {
try {
semaphore.acquire();
//不能使用semaphore.tryAcquire();
System.out.printf("用户[%s]进入窗口\n", user );
Thread.sleep((long) (Math.random()*10000));
System.out.printf("用户[%s]离开窗口\n", user );
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void exec(){
Semaphore semaphore = new Semaphore(2);
ExecutorService executorService = Executors.newCachedThreadPool();
for(int i=0; i<20; i++){
executorService.execute(new SemaphoreRunnable(semaphore, i));
}
}
public static void main(String[] args){
SemaphoreDemo demo = new SemaphoreDemo();
demo.exec();
}
}
java限流(一): Semaphore的更多相关文章
- Java限流策略
概要 在大数据量高并发访问时,经常会出现服务或接口面对暴涨的请求而不可用的情况,甚至引发连锁反映导致整个系统崩溃.此时你需要使用的技术手段之一就是限流,当请求达到一定的并发数或速率,就进行等待.排队. ...
- JUC 中提供的限流利器-Semaphore(信号量)
在 JUC 包下,有一个 Semaphore 类,翻译成信号量,Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源.Semaphore 跟锁 ...
- java限流工具类
代码 import com.google.common.util.concurrent.RateLimiter; import java.util.concurrent.ConcurrentHashM ...
- java高并发系列 - 第15天:JUC中的Semaphore,最简单的限流工具类,必备技能
这是java高并发系列第15篇文章 Semaphore(信号量)为多线程协作提供了更为强大的控制方法,前面的文章中我们学了synchronized和重入锁ReentrantLock,这2种锁一次都只能 ...
- NET 5 原子操作、接口限流Semaphore以及性能测试JMeter
一.Semaphore 限流也可使用令牌桶算法+redis 1.Semaphore定义 类似互斥锁,但它可以允许多个线程同时访问一个共享资源 通过使用一个计数器来控制对共享资源的访问,如果计数器大于0 ...
- 互联网进行限流策略的Semaphore信号量使用
在Semaphore信号量非常适合高并发访问,新系统在上线之前,要对系统的访问量进行评估,当然这个值肯定不是随便拍拍脑袋就能想出来的,是经过以往的经验.数据.历年的访问量,已经推广力度进行一个合理的评 ...
- java 服务接口API限流 Rate Limit
一.场景描述 很多做服务接口的人或多或少的遇到这样的场景,由于业务应用系统的负载能力有限,为了防止非预期的请求对系统压力过大而拖垮业务应用系统. 也就是面对大流量时,如何进行流量控制? 服务接口的流量 ...
- 使用AOP和Semaphore对项目中具体的某一个接口进行限流
整体思路: 一 具体接口,可以自定义一个注解,配置限流量,然后对需要限流的方法加上注解即可! 二 容器初始化的时候扫描所有所有controller,并找出需要限流的接口方法,获取对应的限流量 三 使用 ...
- Java 对IP请求进行限流.
高并发系统下, 有三把利器 缓存 降级 限流. 缓存: 将常用数据缓存起来, 减少数据库或者磁盘IO 降级: 保护核心系统, 降低非核心业务请求响应 限流: 在某一个时间窗口内对请求进行限速, 保护系 ...
随机推荐
- Autofc与Mvc,WebForm,Weiapi,Owin整合源码分析
主要分析一下的几个项目: Autofac.Integration.Mvc Autofac.Integration.WebApi Autofac.Integration.Owin Autofac.Int ...
- tcp/ip ---子网寻址
现在所有的主机都要求支持子网编址( RFC 950 [Mogul and Postel 1985]).不是把I P地址看成由单纯的一个网络号和一个主机号组成,而是把主机号再分成一个子网号和一个主机号. ...
- Android JNI和NDK学习(03)--动态方式实现JNI(转)
本文转自:http://www.cnblogs.com/skywang12345/archive/2013/05/23/3092491.html 前面总结了静态实现JNI的方法,本文介绍如何动态实现J ...
- nginx 模块简介
nginx模块一般分为5类:核心模块.标准http模块.可选http模块.邮件服务模块.第三方模块. 其中核心模块和标准http模块在快速编译之后就包含在了nginx中. 主要看一下核心模块.标准ht ...
- android studio- java注释自己动手弄起来
今天写段子...程序段子.突然觉得AS默认的注释太简洁,不适合自己.于是,自己琢磨半天,大概知道了途径.结果,好好的一个coding之夜也无疾而终了...明天继续跟着包工头学搬砖. 注释设置途径: 1 ...
- mock实例方法
1.Mockito.when(categoryService.queryTopCategory("1")).thenReturn(categories);//返回的是list列表, ...
- Android中makfile的随记
什么是makefile?也许非常多Winodws的程序员都不知道这个东西,由于那些Windows的IDE都为你做了这个工作,但我认为要作一个好的Android底层程序员. makefile还是要懂. ...
- linux命令 — lsof 查看进程打开那些文件 或者 查看文件给那个进程使用
lsof命令是什么? 可以列出被进程所打开的文件的信息.被打开的文件可以是 1.普通的文件,2.目录 3.网络文件系统的文件,4.字符设备文件 5.(函数)共享库 6.管道,命名管道 7.符号链 ...
- Oracle----oracle 事务总结
用了这么长时间的oracle,该总结一下所得了 1,事务 事务用于保证数据的一致性, 它由一组相关的 dml语句组成, 该组的dml(数据操作语言,增删改,没有查询)语句要么全部成功,要么全部失败,比 ...
- 深度历险:Redis 内存模型详解
https://mp.weixin.qq.com/s/Gp6Ur7omGY6ZqDWygU2meQ Redis 是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说 Redi ...