增加分段锁实现SegmentLock(pr#1330@Gitee)

This commit is contained in:
Looly 2025-04-15 11:27:46 +08:00
parent 54d6b99b98
commit 8a17fae3b7
3 changed files with 12 additions and 3 deletions

View File

@ -2,13 +2,14 @@
# 🚀Changelog # 🚀Changelog
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.38(2025-04-11) # 5.8.38(2025-04-15)
### 🐣新特性 ### 🐣新特性
* 【core 】 `PathUtil#del`增加null检查pr#1331@Gitee * 【core 】 `PathUtil#del`增加null检查pr#1331@Gitee
* 【db 】 增加SAP HANA识别及方言pr#3914@Github * 【db 】 增加SAP HANA识别及方言pr#3914@Github
* 【crypto 】 增加`Argon2`实现Argon2算法issue#3890@Github * 【crypto 】 增加`Argon2`实现Argon2算法issue#3890@Github
* 【core 】 `CharSequenceUtil`增加toLoweCase和toUpperCase方法issue#IC0H2B@Gitee * 【core 】 `CharSequenceUtil`增加toLoweCase和toUpperCase方法issue#IC0H2B@Gitee
* 【core 】 增加分段锁实现`SegmentLock`pr#1330@Gitee
### 🐞Bug修复 ### 🐞Bug修复

View File

@ -35,8 +35,9 @@ import java.util.function.Supplier;
* <li>弱引用懒加载首次使用时创建段未使用时可被垃圾回收适合大量段但使用较少的场景</li> * <li>弱引用懒加载首次使用时创建段未使用时可被垃圾回收适合大量段但使用较少的场景</li>
* </ul> * </ul>
* *
* @param <L> 锁类型
* @author Guava,dakuo * @author Guava,dakuo
* @since 5.8.37 * @since 5.8.38
*/ */
public abstract class SegmentLock<L> { public abstract class SegmentLock<L> {
@ -82,7 +83,7 @@ public abstract class SegmentLock<L> {
* @param keys 非空 key 集合 * @param keys 非空 key 集合
* @return 锁段列表可能有重复 * @return 锁段列表可能有重复
*/ */
public Iterable<L> bulkGet(Iterable<? extends Object> keys) { public Iterable<L> bulkGet(Iterable<?> keys) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Object> result = (List<Object>) CollUtil.newArrayList(keys); List<Object> result = (List<Object>) CollUtil.newArrayList(keys);
if (CollUtil.isEmpty(result)) { if (CollUtil.isEmpty(result)) {
@ -268,6 +269,7 @@ public abstract class SegmentLock<L> {
/** /**
* 弱引用安全的条件包装类 * 弱引用安全的条件包装类
*/ */
@SuppressWarnings("FieldCanBeLocal")
private static final class WeakSafeCondition implements Condition { private static final class WeakSafeCondition implements Condition {
private final Condition delegate; private final Condition delegate;
@ -480,6 +482,8 @@ public abstract class SegmentLock<L> {
* 填充锁避免缓存行干扰 * 填充锁避免缓存行干扰
*/ */
private static class PaddedLock extends ReentrantLock { private static class PaddedLock extends ReentrantLock {
private static final long serialVersionUID = 1L;
long unused1; long unused1;
long unused2; long unused2;
long unused3; long unused3;
@ -493,6 +497,8 @@ public abstract class SegmentLock<L> {
* 填充信号量避免缓存行干扰 * 填充信号量避免缓存行干扰
*/ */
private static class PaddedSemaphore extends Semaphore { private static class PaddedSemaphore extends Semaphore {
private static final long serialVersionUID = 1L;
long unused1; long unused1;
long unused2; long unused2;
long unused3; long unused3;

View File

@ -42,6 +42,7 @@ public class SegmentLockTest {
assertEquals(SEGMENT_COUNT, readWriteLock.size()); assertEquals(SEGMENT_COUNT, readWriteLock.size());
} }
@SuppressWarnings("StringOperationCanBeSimplified")
@Test @Test
public void testGetWithSameKey() { public void testGetWithSameKey() {
// 相同 key 应返回相同锁 // 相同 key 应返回相同锁
@ -132,6 +133,7 @@ public class SegmentLockTest {
assertEquals(1, sem.availablePermits(), "释放一个许可后应为 1"); assertEquals(1, sem.availablePermits(), "释放一个许可后应为 1");
} }
@SuppressWarnings("ResultOfMethodCallIgnored")
@Test @Test
public void testReadWriteLock() throws InterruptedException { public void testReadWriteLock() throws InterruptedException {
ReadWriteLock rwLock = readWriteLock.get("testKey"); ReadWriteLock rwLock = readWriteLock.get("testKey");