关于com.alicp.jetcache.Cache#tryLock 方法的线程安全问题,求解,感谢!
- Dominant language
- Java
- Stars
- 5.6k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
你好最近项目中准备应道com.alicp.jetcache.Cache#tryLock的锁,场景主要是进程级别的请求去重。
场景是当前进程多个请求并发请求某个key,发现这个key过期了,此时只需一个线程去刷新缓存即可
但是使用发现并发比较高时,还是有多个线程能够同时占用锁,以下是我的代码:求指教~
private static final Cache cache = CaffeineCacheBuilder.createCaffeineCacheBuilder()
.limit(1000)
.expireAfterWrite(120, SECONDS)
.buildCache();
@Test
public void testLock1() {
for (int i=1; i<=500; i++){
new Thread(() -> {
try(AutoReleaseLock lock = cache.tryLock(key,1, TimeUnit.SECONDS)){
if(lock != null){
// ...
System.out.println(Thread.currentThread().getName() + " " +"get");
}
}
},"thread" + i ).start();
}
// 等待任务结束
try { SECONDS.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}
}
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the Cache#tryLock API and the CaffeineCacheBuilder setup shown in testLock1, then reproduce the behavior with the 500-thread example. Trace the lock acquisition and release path to determine whether multiple holders are expected or indicate a thread-safety defect. Done means the observed behavior is explained and the issue has a confirmed resolution or clearly documented limitation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100