mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
增加分段锁实现SegmentLock
(pr#1330@Gitee)
This commit is contained in:
parent
54d6b99b98
commit
8a17fae3b7
@ -2,13 +2,14 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.38(2025-04-11)
|
||||
# 5.8.38(2025-04-15)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 `PathUtil#del`增加null检查(pr#1331@Gitee)
|
||||
* 【db 】 增加SAP HANA识别及方言(pr#3914@Github)
|
||||
* 【crypto 】 增加`Argon2`类,实现Argon2算法(issue#3890@Github)
|
||||
* 【core 】 `CharSequenceUtil`增加toLoweCase和toUpperCase方法(issue#IC0H2B@Gitee)
|
||||
* 【core 】 增加分段锁实现`SegmentLock`(pr#1330@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
|
||||
|
@ -35,8 +35,9 @@ import java.util.function.Supplier;
|
||||
* <li>弱引用:懒加载,首次使用时创建段,未使用时可被垃圾回收,适合大量段但使用较少的场景。</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <L> 锁类型
|
||||
* @author Guava,dakuo
|
||||
* @since 5.8.37
|
||||
* @since 5.8.38
|
||||
*/
|
||||
public abstract class SegmentLock<L> {
|
||||
|
||||
@ -82,7 +83,7 @@ public abstract class SegmentLock<L> {
|
||||
* @param keys 非空 key 集合
|
||||
* @return 锁段列表(可能有重复)
|
||||
*/
|
||||
public Iterable<L> bulkGet(Iterable<? extends Object> keys) {
|
||||
public Iterable<L> bulkGet(Iterable<?> keys) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Object> result = (List<Object>) CollUtil.newArrayList(keys);
|
||||
if (CollUtil.isEmpty(result)) {
|
||||
@ -268,6 +269,7 @@ public abstract class SegmentLock<L> {
|
||||
/**
|
||||
* 弱引用安全的条件包装类。
|
||||
*/
|
||||
@SuppressWarnings("FieldCanBeLocal")
|
||||
private static final class WeakSafeCondition implements Condition {
|
||||
private final Condition delegate;
|
||||
|
||||
@ -480,6 +482,8 @@ public abstract class SegmentLock<L> {
|
||||
* 填充锁,避免缓存行干扰。
|
||||
*/
|
||||
private static class PaddedLock extends ReentrantLock {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
long unused1;
|
||||
long unused2;
|
||||
long unused3;
|
||||
@ -493,6 +497,8 @@ public abstract class SegmentLock<L> {
|
||||
* 填充信号量,避免缓存行干扰。
|
||||
*/
|
||||
private static class PaddedSemaphore extends Semaphore {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
long unused1;
|
||||
long unused2;
|
||||
long unused3;
|
||||
|
@ -42,6 +42,7 @@ public class SegmentLockTest {
|
||||
assertEquals(SEGMENT_COUNT, readWriteLock.size());
|
||||
}
|
||||
|
||||
@SuppressWarnings("StringOperationCanBeSimplified")
|
||||
@Test
|
||||
public void testGetWithSameKey() {
|
||||
// 相同 key 应返回相同锁
|
||||
@ -132,6 +133,7 @@ public class SegmentLockTest {
|
||||
assertEquals(1, sem.availablePermits(), "释放一个许可后应为 1");
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
@Test
|
||||
public void testReadWriteLock() throws InterruptedException {
|
||||
ReadWriteLock rwLock = readWriteLock.get("testKey");
|
||||
|
Loading…
x
Reference in New Issue
Block a user