sofastack / sofa-jraft

A production-grade java implementation of RAFT consensus algorithm.
https://www.sofastack.tech/projects/sofa-jraft/
Apache License 2.0
3.59k stars 1.15k forks source link

这个分布式锁能实现阻塞唤醒的功能吗 #1134

Closed chunlaiqingke closed 2 months ago

chunlaiqingke commented 3 months ago

rt,这个分布式锁能实现阻塞等待吗,阻塞和唤醒的效果吗

fengjiachun commented 3 months ago

没看懂问题

chunlaiqingke commented 3 months ago

就是2个进程去拿锁,一个拿到锁,一个没拿到,我希望没拿到锁的进程阻塞,等锁的释放

fengjiachun commented 3 months ago

必然可以啊,用这个带 timeout 参数的

https://github.com/sofastack/sofa-jraft/blob/2d0495524d80210e0982c6fb4b0f3de5f17dc6ae/jraft-rheakv/rheakv-core/src/main/java/com/alipay/sofa/jraft/rhea/util/concurrent/DistributedLock.java#L89

en-hui commented 2 months ago

如果不知道timeout弄多少,应该也可以自己在客户端用的时候封装一下

public void lock() { long start = System.currentTimeMillis(); while (!rheaKvLock.tryLock()) { if (System.currentTimeMillis() - start > 10 * 1000L) { log.warn("[JRaft RheaKV] try lock {} 10s", lockName); start = System.currentTimeMillis(); } try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RheaKVException(e); } } log.warn("[JRaft RheaKV] try lock {} success", lockName); }