Nitesh Jain
Nitesh Jain

Reputation: 177

ReentrantReadWriteLock - Not able to acquire write lock, because of read lock

I'm facing a problem while acquiring write lock and then do some operation using ReentrantReadWriteLock

There are two classes say ClassA and ClassB ClassA is holding write lock ClassB is having read lock

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();
   }
  }
 }
}

There is an UI where in user can update/create, when this event happens i am activating write lock in ClassA, so that readers of ClassB be blocked.

but for some strange reason, ClassA is waiting(infinitely) to acquire Write lock!!

Can we reset all the lock acquired by different threads and then acquire write lock ? please suggest if there is another way around..

Upvotes: 0

Views: 875

Answers (0)

Related Questions