public class Concurrents extends Object
| Constructor and Description |
|---|
Concurrents() |
| Modifier and Type | Method and Description |
|---|---|
static CountDownLatch |
countDownLatch(int count)
返回CountDownLatch, 每条线程减1,减到0时正在latch.wait()的进程继续进行
|
static CyclicBarrier |
cyclicBarrier(int count)
返回CyclicBarrier,每条线程减1并等待,减到0时,所有线程继续运行
|
static Semaphore |
fairSemaphore(int permits)
返回公平的信号量,先请求的线程先拿到信号量
|
static LongAdder |
longAdder()
返回没有激烈CAS冲突的LongAdder, 并发的+1将在不同的Counter里进行,只在取值时将多个Counter求和.
|
static Semaphore |
nonFairSemaphore(int permits)
返回默认的非公平信号量,先请求的线程不一定先拿到信号量
|
static com.google.common.util.concurrent.RateLimiter |
rateLimiter(int permitsPerSecond)
返回令牌桶算法的RateLimiter默认版,默认令牌桶大小等于期望的QPS,且刚启动时桶为空。
|
static com.google.common.util.concurrent.RateLimiter |
rateLimiter(int permitsPerSecond,
int maxBurstSeconds)
返回令牌桶算法的RateLimiter定制版,可定制令牌桶的大小,且刚启动时桶已装满。
|
static Sampler |
sampler(double selectPercent)
返回采样器.
|
static TimeIntervalLimiter |
timeIntervalLimiter(long interval,
TimeUnit timeUnit)
返回时间间隔限制器.
|
public static LongAdder longAdder()
public static CountDownLatch countDownLatch(int count)
public static CyclicBarrier cyclicBarrier(int count)
public static Semaphore nonFairSemaphore(int permits)
public static Semaphore fairSemaphore(int permits)
public static com.google.common.util.concurrent.RateLimiter rateLimiter(int permitsPerSecond)
public static com.google.common.util.concurrent.RateLimiter rateLimiter(int permitsPerSecond,
int maxBurstSeconds)
throws ReflectiveOperationException
permitsPerSecond - 每秒允许的请求数,可看成QPS,同时将QPS平滑到毫秒级别上,请求到达速度不平滑时依赖缓冲能力.maxBurstSeconds - 可看成桶的容量,Guava中最大的突发流量缓冲时间,默认是1s, permitsPerSecond * maxBurstSeconds,就是闲时能累积的缓冲token最大数量。ReflectiveOperationExceptionpublic static Sampler sampler(double selectPercent)
selectPercent - 采样率,在0-100 之间,可以有小数位public static TimeIntervalLimiter timeIntervalLimiter(long interval, TimeUnit timeUnit)
interval - 间隔时间timeUnit - 间隔时间单位Copyright © 2018. All rights reserved.