Look at the following code...
public class Main { public static void main(String[] args) throws Exception { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); ReadLock rlock = lock.readLock(); WriteLock wlock = lock.writeLock(); try { System.out.println("Aquiring read lock..."); rlock.lock(); System.out.println("Aquiring write lock..."); //gets blocked here wlock.lock(); System.out.println("Got write lock as well"); } finally { wlock.unlock(); rlock.unlock(); } System.out.println("Finished."); } }Above code gets blocked at wlock.lock() as read lock can not be upgraded to write lock.
Whats worse is that other threads will now block even to acquire a read lock because main thread is waiting to acquire a write lock. Essentially, with all likelihood, your whole application will stop working ;).
Here is the code demonstrating that other thread will block even for a read lock when nobody really has a write lock(and this behavior is implemented to save the thread, waiting to acquire write lock, from starving).
public class Main { public static void main(String[] args) throws Exception { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); ReadLock rlock = lock.readLock(); WriteLock wlock = lock.writeLock(); try { System.out.println("Aquiring read lock..."); rlock.lock(); tryAcquiringReadLockInAnotherThread(rlock); System.out.println("Aquiring write lock..."); //gets blocked here wlock.lock(); System.out.println("Got write lock as well"); } finally { wlock.unlock(); rlock.unlock(); } System.out.println("Finished."); } private static void tryAcquiringReadLockInAnotherThread(final ReadLock rlock) { Thread t = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); System.out.println("Trying to aquire read lock in other thread..."); //gets blocked here rlock.lock(); System.out.println("Acquired read lock in other thread."); } catch(InterruptedException ex) { //do not do this in real program :) ex.printStackTrace(); }finally { rlock.unlock(); } } }); t.start(); } }
I tried it on following java version..
$ java -version java version "1.6.0_30" Java(TM) SE Runtime Environment (build 1.6.0_30-b12) Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)Ref: ReentrantReadWriteLock
No comments:
Post a Comment