X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fconcurrency%2FReadWriteLock.java;h=8afe55ec9043ba32b60a32c9c8138015a9302d2e;hb=4a575582a5c2999bd816b197d9cf274b4b3ddcd7;hp=bfa4d311a02862162ef6fd5640313a1893daf876;hpb=0d8d8f24656e585ee75558cfd6a4c661f8f14985;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 bfa4d311..8afe55ec 100644 --- a/support/general/src/main/java/org/wamblee/concurrency/ReadWriteLock.java +++ b/support/general/src/main/java/org/wamblee/concurrency/ReadWriteLock.java @@ -1,5 +1,5 @@ /* - * Copyright 2005 the original author or authors. + * Copyright 2005-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,24 +12,28 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ + */ package org.wamblee.concurrency; import java.util.HashSet; - /** * Read-write lock for allowing multiple concurrent readers or at most one * writer. This implementation does not aim for high performance but for * robustness and simplicity. Users of this class should not synchronize on * objects of this class. + * + * This class was originally developed for a Java developer certification. It is + * deprecated now and {@link java.util.concurrent.locks.ReadWriteLock} should be + * used instead. */ +@Deprecated public class ReadWriteLock { /** - * Sets containing the references to the threads that are currently - * reading. This administration is useful to check that the lock has - * already been acquired before it is release. This check adds robustness - * to the application. + * Sets containing the references to the threads that are currently reading. + * This administration is useful to check that the lock has already been + * acquired before it is release. This check adds robustness to the + * application. */ private HashSet readers; @@ -43,28 +47,28 @@ public class ReadWriteLock { * Constructs read-write lock. */ public ReadWriteLock() { - readers = new HashSet(); - writer = null; + readers = new HashSet(); + writer = null; } /** - * Acquires the lock for reading. This call will block until the lock can - * be acquired. - * - * @throws IllegalStateException Thrown if the read or write lock is - * already acquired. + * Acquires the lock for reading. This call will block until the lock can be + * acquired. + * + * @throws IllegalStateException + * Thrown if the read or write lock is already acquired. */ public synchronized void acquireRead() { if (readers.contains(Thread.currentThread())) { throw new IllegalStateException( - "Read lock already acquired by current thread: " - + Thread.currentThread()); + "Read lock already acquired by current thread: " + + Thread.currentThread()); } if (writer == Thread.currentThread()) { throw new IllegalStateException( - "Trying to acquire the read lock while already holding a write lock: " - + Thread.currentThread()); + "Trying to acquire the read lock while already holding a write lock: " + + Thread.currentThread()); } while (writer != null) { @@ -79,11 +83,11 @@ public class ReadWriteLock { } /** - * Releases the lock for reading. Note: This implementation assumes that - * the lock has already been acquired for reading previously. - * - * @throws IllegalStateException Thrown when the lock was not acquired by - * this thread. + * Releases the lock for reading. Note: This implementation assumes that the + * lock has already been acquired for reading previously. + * + * @throws IllegalStateException + * Thrown when the lock was not acquired by this thread. */ public synchronized void releaseRead() { if (!readers.remove(Thread.currentThread())) { @@ -99,25 +103,25 @@ public class ReadWriteLock { /** * Acquires the lock for writing. This call will block until the lock has * been acquired. - * - * @throws IllegalStateException Thrown if the read or write lock is - * already acquired. + * + * @throws IllegalStateException + * Thrown if the read or write lock is already acquired. */ public synchronized void acquireWrite() { if (writer == Thread.currentThread()) { throw new IllegalStateException( - "Trying to acquire a write lock while already holding the write lock: " - + Thread.currentThread()); + "Trying to acquire a write lock while already holding the write lock: " + + Thread.currentThread()); } if (readers.contains(Thread.currentThread())) { throw new IllegalStateException( - "Trying to acquire a write lock while already holding the read lock: " - + Thread.currentThread()); + "Trying to acquire a write lock while already holding the read lock: " + + Thread.currentThread()); } // wait until there are no more writers and no more - // readers + // readers while ((writer != null) || (readers.size() > 0)) { try { wait(); @@ -134,8 +138,9 @@ public class ReadWriteLock { /** * Releases the lock for writing. - * - * @throws IllegalStateException Thrown when the lock was not acquired. + * + * @throws IllegalStateException + * Thrown when the lock was not acquired. */ public synchronized void releaseWrite() { if (writer != Thread.currentThread()) {