X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fconcurrency%2FReadWriteLockTest.java;h=2e7996244752d68de4d659e1fa270821e79615d2;hb=ddd261f331280640c5b53c7128230b629ebcd268;hp=e0d22f774923620b3e2a9ade3c7042bc4bf51b2e;hpb=92e23e5ecf9614f2ab770a8cdedc0b21ddf1e127;p=utils diff --git a/support/general/src/test/java/org/wamblee/concurrency/ReadWriteLockTest.java b/support/general/src/test/java/org/wamblee/concurrency/ReadWriteLockTest.java index e0d22f77..2e799624 100644 --- a/support/general/src/test/java/org/wamblee/concurrency/ReadWriteLockTest.java +++ b/support/general/src/test/java/org/wamblee/concurrency/ReadWriteLockTest.java @@ -1,18 +1,18 @@ /* * Copyright 2005 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. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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 junit.framework.Assert; @@ -27,20 +27,22 @@ import junit.framework.TestCase; */ 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; - private int nReaders; - private int nWriters; + private int nReaders; + private int nWriters; /** * Constructor for ReadWriteLockTest. @@ -115,7 +117,7 @@ public class ReadWriteLockTest extends TestCase { public void testMultipleReaders() throws InterruptedException { Runnable runnable = new ReadLocker(lock, this, TWO_SECONDS); - Thread t1 = new Thread(runnable); + Thread t1 = new Thread(runnable); t1.start(); Thread t2 = new Thread(runnable); @@ -133,8 +135,8 @@ public class ReadWriteLockTest extends TestCase { */ public void testSingleWriter() throws InterruptedException { WriteLocker writer = new WriteLocker(lock, this, ONE_SECOND); - Thread t1 = new Thread(writer); - Thread t2 = new Thread(writer); + Thread t1 = new Thread(writer); + Thread t2 = new Thread(writer); t1.start(); t2.start(); @@ -151,10 +153,11 @@ public class ReadWriteLockTest extends TestCase { * @throws InterruptedException May not occur. */ public void testMultipleWriters() throws InterruptedException { - WriteLocker writer1 = new WriteLocker(lock, this, HALF_SECOND + ONE_SECOND); + 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); + Thread t1 = new Thread(writer1); + Thread t2 = new Thread(writer2); t1.start(); Thread.sleep(HALF_SECOND); @@ -180,10 +183,10 @@ public class ReadWriteLockTest extends TestCase { * @throws InterruptedException May not occur. */ public void testReadWrite1() throws InterruptedException { - ReadLocker readLocker = new ReadLocker(lock, this, TWO_SECONDS); - Thread t1 = new Thread(readLocker); + ReadLocker readLocker = new ReadLocker(lock, this, TWO_SECONDS); + Thread t1 = new Thread(readLocker); WriteLocker writeLocker = new WriteLocker(lock, this, TWO_SECONDS); - Thread t2 = new Thread(writeLocker); + Thread t2 = new Thread(writeLocker); t1.start(); // acquire read lock Thread.sleep(HALF_SECOND); @@ -213,12 +216,14 @@ public class ReadWriteLockTest extends TestCase { * @throws InterruptedException May not occur. */ public void testReadWrite2() throws InterruptedException { - 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); + 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, TWO_SECONDS); - Thread t3 = new Thread(writeLocker); + Thread t3 = new Thread(writeLocker); t1.start(); // acquire read lock [0, 2.5] Thread.sleep(ONE_SECOND); @@ -259,10 +264,10 @@ public class ReadWriteLockTest extends TestCase { * @throws InterruptedException May not occur. */ public void testReadWrite3() throws InterruptedException { - ReadLocker readLocker = new ReadLocker(lock, this, TWO_SECONDS); - Thread t1 = new Thread(readLocker); + ReadLocker readLocker = new ReadLocker(lock, this, TWO_SECONDS); + Thread t1 = new Thread(readLocker); WriteLocker writeLocker = new WriteLocker(lock, this, TWO_SECONDS); - Thread t2 = new Thread(writeLocker); + Thread t2 = new Thread(writeLocker); t2.start(); // acquire write lock Thread.sleep(HALF_SECOND); @@ -447,15 +452,15 @@ public class ReadWriteLockTest extends TestCase { * lock, and performs a callback after the lock has been released. */ class ReadLocker implements Runnable { - private ReadWriteLock lock; + private ReadWriteLock lock; private ReadWriteLockTest lockTest; - private int sleepTime; + private int sleepTime; public ReadLocker(ReadWriteLock aLock, ReadWriteLockTest aLockTest, int aSleepTime) { - lock = aLock; - lockTest = aLockTest; - sleepTime = aSleepTime; + lock = aLock; + lockTest = aLockTest; + sleepTime = aSleepTime; } public void run() { @@ -465,8 +470,8 @@ class ReadLocker implements Runnable { try { Thread.sleep(sleepTime); } catch (InterruptedException e) { - Assert.fail("ReadLocker thread was interrupted." - + Thread.currentThread()); + Assert.fail("ReadLocker thread was interrupted." + + Thread.currentThread()); } lock.releaseRead(); @@ -481,15 +486,15 @@ class ReadLocker implements Runnable { * lock, and performs a callback after the lock has been released. */ class WriteLocker implements Runnable { - private ReadWriteLock lock; + private ReadWriteLock lock; private ReadWriteLockTest lockTest; - private int sleepTime; + private int sleepTime; public WriteLocker(ReadWriteLock aLock, ReadWriteLockTest aLockTest, int aSleepTime) { - lock = aLock; - lockTest = aLockTest; - sleepTime = aSleepTime; + lock = aLock; + lockTest = aLockTest; + sleepTime = aSleepTime; } public void run() { @@ -499,8 +504,8 @@ class WriteLocker implements Runnable { try { Thread.sleep(sleepTime); } catch (InterruptedException e) { - Assert.fail("WriteLocker thread was interrupted: " - + Thread.currentThread()); + Assert.fail("WriteLocker thread was interrupted: " + + Thread.currentThread()); } lock.releaseWrite();