public class LatchLock extends Object
| Modifier and Type | Field and Description |
|---|---|
protected AtomicInteger |
latchSize
当前并发大小
|
protected int |
limit
流量限制大小
|
| Constructor and Description |
|---|
LatchLock() |
| Modifier and Type | Method and Description |
|---|---|
int |
getLatchSize()
当前已获取锁个数
|
LatchLock |
limit(int limit)
设置限制
|
int |
release()
释放一个锁
一般是
tryLock() 成功后 调一次 |
boolean |
tryLock()
获取一个锁
|
protected int limit
protected final AtomicInteger latchSize
public LatchLock limit(int limit)
limit - >0public boolean tryLock()
final LatchLock lock = new LatchLock();
lock.limit(3); // 设置并发限制. 默认为1
if (lock.tryLock()) { // 尝试获取一个锁
try {
// 被执行的代码块
} finally {
lock.release(); // 释放一个锁
}
}
public int release()
tryLock() 成功后 调一次public int getLatchSize()
Copyright © 2022. All rights reserved.