X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fconcurrency%2FReadWriteLock.java;fp=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fconcurrency%2FReadWriteLock.java;h=bfa4d311a02862162ef6fd5640313a1893daf876;hb=0d8d8f24656e585ee75558cfd6a4c661f8f14985;hp=e765816133ed87d4dce3c39daa247fa8cf7250de;hpb=da48a523c81e59fe0eac34e43d12937396161f25;p=utils diff --git a/support/general/src/main/java/org/wamblee/concurrency/ReadWriteLock.java b/support/general/src/main/java/org/wamblee/concurrency/ReadWriteLock.java index e7658161..bfa4d311 100644 --- a/support/general/src/main/java/org/wamblee/concurrency/ReadWriteLock.java +++ b/support/general/src/main/java/org/wamblee/concurrency/ReadWriteLock.java @@ -31,20 +31,20 @@ public class ReadWriteLock { * already been acquired before it is release. This check adds robustness * to the application. */ - private HashSet _readers; + private HashSet readers; /** * The thread that has acquired the lock for writing or null if no such * thread exists currently. */ - private Thread _writer; + private Thread writer; /** * Constructs read-write lock. */ public ReadWriteLock() { - _readers = new HashSet(); - _writer = null; + readers = new HashSet(); + writer = null; } /** @@ -55,19 +55,19 @@ public class ReadWriteLock { * already acquired. */ public synchronized void acquireRead() { - if (_readers.contains(Thread.currentThread())) { + if (readers.contains(Thread.currentThread())) { throw new IllegalStateException( "Read lock already acquired by current thread: " + Thread.currentThread()); } - if (_writer == Thread.currentThread()) { + if (writer == Thread.currentThread()) { throw new IllegalStateException( "Trying to acquire the read lock while already holding a write lock: " + Thread.currentThread()); } - while (_writer != null) { + while (writer != null) { try { wait(); } catch (InterruptedException e) { @@ -75,7 +75,7 @@ public class ReadWriteLock { } } - _readers.add(Thread.currentThread()); + readers.add(Thread.currentThread()); } /** @@ -86,12 +86,12 @@ public class ReadWriteLock { * this thread. */ public synchronized void releaseRead() { - if (!_readers.remove(Thread.currentThread())) { + if (!readers.remove(Thread.currentThread())) { throw new IllegalStateException( "Cannot release read lock because current thread has not acquired it."); } - if (_readers.size() == 0) { + if (readers.size() == 0) { notifyAll(); } } @@ -104,13 +104,13 @@ public class ReadWriteLock { * already acquired. */ public synchronized void acquireWrite() { - if (_writer == Thread.currentThread()) { + if (writer == Thread.currentThread()) { throw new IllegalStateException( "Trying to acquire a write lock while already holding the write lock: " + Thread.currentThread()); } - if (_readers.contains(Thread.currentThread())) { + if (readers.contains(Thread.currentThread())) { throw new IllegalStateException( "Trying to acquire a write lock while already holding the read lock: " + Thread.currentThread()); @@ -118,7 +118,7 @@ public class ReadWriteLock { // wait until there are no more writers and no more // readers - while ((_writer != null) || (_readers.size() > 0)) { + while ((writer != null) || (readers.size() > 0)) { try { wait(); } catch (InterruptedException e) { @@ -126,7 +126,7 @@ public class ReadWriteLock { } } - _writer = Thread.currentThread(); + writer = Thread.currentThread(); // notification not necessary since all writers and // readers are now blocked by this thread. @@ -138,12 +138,12 @@ public class ReadWriteLock { * @throws IllegalStateException Thrown when the lock was not acquired. */ public synchronized void releaseWrite() { - if (_writer != Thread.currentThread()) { + if (writer != Thread.currentThread()) { throw new IllegalStateException( "Cannot release write lock because it was not acquired. "); } - _writer = null; + writer = null; notifyAll(); } }