0

我在获取写锁时遇到问题,然后使用ReentrantReadWriteLock进行一些操作

有两个类说 ClassA 和 ClassB ClassA 持有写锁 ClassB 有读锁

public static final ReentrantReadWriteLock lock=new ReentrantReadWriteLock(); //defined in singleton class

ClassA{

 public void onMessage(Message msg){
  if(update is received from UI){
   lock.writeLock().lock();
   try{
     //critical code here
   }finally{
     lock.writeLock().unlock();
   }
 }
}
}

ClassB{
public void onMessage(Message msg){
  if(NO update is received from UI){
   lock.readLock().lock();
   try{
     //critical code here
   }finally{
     lock.readLock().unlock();
   }
  }
 }
}

用户可以在其中更新/创建一个 UI,当此事件发生时,我正在激活 ClassA 中的写锁,以便阻止 ClassB 的读者。

但出于某种奇怪的原因,ClassA 正在等待(无限地)获取写锁!

我们可以重置不同线程获取的所有锁,然后获取写锁吗?请建议是否有其他方法..

4

0 回答 0