* @see ReadWriteLock
*/
public class ReadWriteLockTest extends TestCase {
+ /**
+ *
+ */
+ private static final int HALF_SECOND = 500;
+ /**
+ *
+ */
+ private static final int ONE_SECOND = 1000;
+ /**
+ *
+ */
+ private static final int TWO_SECONDS = 2000;
private ReadWriteLock _lock;
- int _nReaders;
- int _nWriters;
+ private int _nReaders;
+ private int _nWriters;
/**
* Constructor for ReadWriteLockTest.
* @throws InterruptedException May not occur.
*/
public void testMultipleReaders() throws InterruptedException {
- Runnable runnable = new ReadLocker(_lock, this, 2000);
+ Runnable runnable = new ReadLocker(_lock, this, TWO_SECONDS);
Thread t1 = new Thread(runnable);
t1.start();
Thread t2 = new Thread(runnable);
t2.start();
- Thread.sleep(1000);
+ Thread.sleep(ONE_SECOND);
assertTrue("Not enough readers!", getReaderCount() == 2);
t1.join();
t2.join();
* @throws InterruptedException May not occur.
*/
public void testSingleWriter() throws InterruptedException {
- WriteLocker writer = new WriteLocker(_lock, this, 1000);
+ WriteLocker writer = new WriteLocker(_lock, this, ONE_SECOND);
Thread t1 = new Thread(writer);
Thread t2 = new Thread(writer);
t1.start();
t2.start();
- Thread.sleep(500);
+ Thread.sleep(HALF_SECOND);
assertTrue("Wrong writer count: " + getWriterCount(),
getWriterCount() == 1);
t1.join();
* @throws InterruptedException May not occur.
*/
public void testMultipleWriters() throws InterruptedException {
- WriteLocker writer1 = new WriteLocker(_lock, this, 1500);
- WriteLocker writer2 = new WriteLocker(_lock, this, 1000);
+ WriteLocker writer1 = new WriteLocker(_lock, this, HALF_SECOND + ONE_SECOND);
+ WriteLocker writer2 = new WriteLocker(_lock, this, ONE_SECOND);
Thread t1 = new Thread(writer1);
Thread t2 = new Thread(writer2);
t1.start();
- Thread.sleep(500);
+ Thread.sleep(HALF_SECOND);
assertTrue(getWriterCount() == 1);
t2.start();
- Thread.sleep(500);
+ Thread.sleep(HALF_SECOND);
assertTrue(getWriterCount() == 1); // first writer still
// has the lock.
- Thread.sleep(1000);
+ Thread.sleep(ONE_SECOND);
// at t = 2, the second writer still must have
// a lock.
* @throws InterruptedException May not occur.
*/
public void testReadWrite1() throws InterruptedException {
- ReadLocker readLocker = new ReadLocker(_lock, this, 2000);
+ ReadLocker readLocker = new ReadLocker(_lock, this, TWO_SECONDS);
Thread t1 = new Thread(readLocker);
- WriteLocker writeLocker = new WriteLocker(_lock, this, 2000);
+ WriteLocker writeLocker = new WriteLocker(_lock, this, TWO_SECONDS);
Thread t2 = new Thread(writeLocker);
t1.start(); // acquire read lock
- Thread.sleep(500);
+ Thread.sleep(HALF_SECOND);
assertTrue(getReaderCount() == 1);
t2.start();
- Thread.sleep(500);
+ Thread.sleep(HALF_SECOND);
// 1 second underway, reader still holding the
// lock so write lock cannot be acquired.
assertTrue(getReaderCount() == 1);
assertTrue(getWriterCount() == 0);
- Thread.sleep(1500);
+ Thread.sleep(ONE_SECOND + HALF_SECOND);
// 2.5 seconds underway, read lock released and
// write lock must be acquired.
* @throws InterruptedException May not occur.
*/
public void testReadWrite2() throws InterruptedException {
- ReadLocker readLocker1 = new ReadLocker(_lock, this, 2500);
- ReadLocker readLocker2 = new ReadLocker(_lock, this, 2500);
+ ReadLocker readLocker1 = new ReadLocker(_lock, this, TWO_SECONDS + HALF_SECOND);
+ ReadLocker readLocker2 = new ReadLocker(_lock, this, TWO_SECONDS + HALF_SECOND);
Thread t1 = new Thread(readLocker1);
Thread t2 = new Thread(readLocker2);
- WriteLocker writeLocker = new WriteLocker(_lock, this, 2000);
+ WriteLocker writeLocker = new WriteLocker(_lock, this, TWO_SECONDS);
Thread t3 = new Thread(writeLocker);
t1.start(); // acquire read lock
- Thread.sleep(1000);
+ Thread.sleep(ONE_SECOND);
assertTrue(getReaderCount() == 1);
t2.start();
- Thread.sleep(500);
+ Thread.sleep(HALF_SECOND);
assertTrue(getReaderCount() == 2);
t3.start();
- Thread.sleep(500);
+ Thread.sleep(HALF_SECOND);
// 2 seconds,
assertTrue(getReaderCount() == 2);
assertTrue(getWriterCount() == 0);
- Thread.sleep(1000);
+ Thread.sleep(ONE_SECOND);
// 3 seconds underway, first read lock must
// have been released.
assertTrue(getReaderCount() == 1);
assertTrue(getWriterCount() == 0);
- Thread.sleep(500);
+ Thread.sleep(HALF_SECOND);
// 4 seconds underway, write lock must have
// been acquired.
* @throws InterruptedException May not occur.
*/
public void testReadWrite3() throws InterruptedException {
- ReadLocker readLocker = new ReadLocker(_lock, this, 2000);
+ ReadLocker readLocker = new ReadLocker(_lock, this, TWO_SECONDS);
Thread t1 = new Thread(readLocker);
- WriteLocker writeLocker = new WriteLocker(_lock, this, 2000);
+ WriteLocker writeLocker = new WriteLocker(_lock, this, TWO_SECONDS);
Thread t2 = new Thread(writeLocker);
t2.start(); // acquire write lock
- Thread.sleep(500);
+ Thread.sleep(HALF_SECOND);
assertTrue(getWriterCount() == 1);
t1.start();
- Thread.sleep(500);
+ Thread.sleep(HALF_SECOND);
// 1 second underway, writer still holding the
// lock so read lock cannot be acquired.
assertTrue(getWriterCount() == 1);
assertTrue(getReaderCount() == 0);
- Thread.sleep(1500);
+ Thread.sleep(ONE_SECOND + HALF_SECOND);
// 2.5 seconds underway, write lock released and
// read lock must be acquired.
}
});
t1.start();
- Thread.sleep(1000); // wait until thread is started
+ Thread.sleep(ONE_SECOND); // wait until thread is started
_lock.releaseRead(); // release lock from wrong thread.
} catch (RuntimeException e) {
return; // ok
}
});
t1.start();
- Thread.sleep(1000); // wait until thread is started
+ Thread.sleep(ONE_SECOND); // wait until thread is started
_lock.releaseWrite(); // release lock from wrong thread.
} catch (RuntimeException e) {
return; // ok
* lock, and performs a callback after the lock has been released.
*/
class ReadLocker implements Runnable {
- ReadWriteLock _lock;
- ReadWriteLockTest _lockTest;
- int _sleepTime;
-
- public ReadLocker(ReadWriteLock lock, ReadWriteLockTest lockTest,
- int sleepTime) {
- _lock = lock;
- _lockTest = lockTest;
- _sleepTime = sleepTime;
+ private ReadWriteLock _lock;
+ private ReadWriteLockTest _lockTest;
+ private int _sleepTime;
+
+ public ReadLocker(ReadWriteLock aLock, ReadWriteLockTest aLockTest,
+ int aSleepTime) {
+ _lock = aLock;
+ _lockTest = aLockTest;
+ _sleepTime = aSleepTime;
}
public void run() {
* lock, and performs a callback after the lock has been released.
*/
class WriteLocker implements Runnable {
- ReadWriteLock _lock;
- ReadWriteLockTest _lockTest;
- int _sleepTime;
-
- public WriteLocker(ReadWriteLock lock, ReadWriteLockTest lockTest,
- int sleepTime) {
- _lock = lock;
- _lockTest = lockTest;
- _sleepTime = sleepTime;
+ private ReadWriteLock _lock;
+ private ReadWriteLockTest _lockTest;
+ private int _sleepTime;
+
+ public WriteLocker(ReadWriteLock aLock, ReadWriteLockTest aLockTest,
+ int aSleepTime) {
+ _lock = aLock;
+ _lockTest = aLockTest;
+ _sleepTime = aSleepTime;
}
public void run() {