--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2006/05/18 10:29:23 1.21 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2009/11/02 20:28:31 1.22 @@ -2,8 +2,8 @@ * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain - * Other contributors include Andrew Wright, Jeffrey Hayes, - * Pat Fisher, Mike Judd. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ @@ -31,33 +31,33 @@ public class AbstractQueuedSynchronizerT */ static class Mutex extends AbstractQueuedSynchronizer { public boolean isHeldExclusively() { return getState() == 1; } - + public boolean tryAcquire(int acquires) { - assertTrue(acquires == 1); + assertTrue(acquires == 1); return compareAndSetState(0, 1); } - + public boolean tryRelease(int releases) { if (getState() == 0) throw new IllegalMonitorStateException(); setState(0); return true; } - + public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } } - + /** * A simple latch class, to test shared mode. */ - static class BooleanLatch extends AbstractQueuedSynchronizer { + static class BooleanLatch extends AbstractQueuedSynchronizer { public boolean isSignalled() { return getState() != 0; } public int tryAcquireShared(int ignore) { return isSignalled()? 1 : -1; } - + public boolean tryReleaseShared(int ignore) { setState(1); return true; @@ -96,15 +96,15 @@ public class AbstractQueuedSynchronizerT /** * isHeldExclusively is false upon construction */ - public void testIsHeldExclusively() { + public void testIsHeldExclusively() { Mutex rl = new Mutex(); assertFalse(rl.isHeldExclusively()); } - + /** * acquiring released sync succeeds */ - public void testAcquire() { + public void testAcquire() { Mutex rl = new Mutex(); rl.acquire(1); assertTrue(rl.isHeldExclusively()); @@ -115,7 +115,7 @@ public class AbstractQueuedSynchronizerT /** * tryAcquire on an released sync succeeds */ - public void testTryAcquire() { + public void testTryAcquire() { Mutex rl = new Mutex(); assertTrue(rl.tryAcquire(1)); assertTrue(rl.isHeldExclusively()); @@ -125,7 +125,7 @@ public class AbstractQueuedSynchronizerT /** * hasQueuedThreads reports whether there are waiting threads */ - public void testhasQueuedThreads() { + public void testhasQueuedThreads() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -149,12 +149,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * isQueued(null) throws NPE */ - public void testIsQueuedNPE() { + public void testIsQueuedNPE() { final Mutex sync = new Mutex(); try { sync.isQueued(null); @@ -166,7 +166,7 @@ public class AbstractQueuedSynchronizerT /** * isQueued reports whether a thread is queued. */ - public void testIsQueued() { + public void testIsQueued() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -195,12 +195,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * getFirstQueuedThread returns first waiting thread or null if none */ - public void testGetFirstQueuedThread() { + public void testGetFirstQueuedThread() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -225,13 +225,13 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * hasContended reports false if no thread has ever blocked, else true */ - public void testHasContended() { + public void testHasContended() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -255,12 +255,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * getQueuedThreads includes waiting threads */ - public void testGetQueuedThreads() { + public void testGetQueuedThreads() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -287,12 +287,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * getExclusiveQueuedThreads includes waiting threads */ - public void testGetExclusiveQueuedThreads() { + public void testGetExclusiveQueuedThreads() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -319,12 +319,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * getSharedQueuedThreads does not include exclusively waiting threads */ - public void testGetSharedQueuedThreads() { + public void testGetSharedQueuedThreads() { final Mutex sync = new Mutex(); Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); @@ -349,12 +349,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * tryAcquireNanos is interruptible. */ - public void testInterruptedException2() { + public void testInterruptedException2() { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new Runnable() { @@ -377,7 +377,7 @@ public class AbstractQueuedSynchronizerT /** * TryAcquire on exclusively held sync fails */ - public void testTryAcquireWhenSynced() { + public void testTryAcquireWhenSynced() { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new Runnable() { @@ -392,12 +392,12 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * tryAcquireNanos on an exclusively held sync times out */ - public void testAcquireNanos_Timeout() { + public void testAcquireNanos_Timeout() { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new Runnable() { @@ -416,9 +416,9 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } - - + } + + /** * getState is true when acquired and false when not */ @@ -428,7 +428,7 @@ public class AbstractQueuedSynchronizerT assertTrue(sync.isHeldExclusively()); sync.release(1); assertFalse(sync.isHeldExclusively()); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { sync.acquire(1); try { @@ -455,7 +455,7 @@ public class AbstractQueuedSynchronizerT /** * acquireInterruptibly is interruptible. */ - public void testAcquireInterruptibly1() { + public void testAcquireInterruptibly1() { final Mutex sync = new Mutex(); sync.acquire(1); Thread t = new Thread(new InterruptedSyncRunnable(sync)); @@ -469,13 +469,13 @@ public class AbstractQueuedSynchronizerT } catch(Exception e){ unexpectedException(); } - } + } /** * acquireInterruptibly succeeds when released, else is interruptible */ public void testAcquireInterruptibly2() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); try { sync.acquireInterruptibly(1); } catch(Exception e) { @@ -496,7 +496,7 @@ public class AbstractQueuedSynchronizerT * owns is true for a condition created by sync else false */ public void testOwns() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); final Mutex sync2 = new Mutex(); assertTrue(sync.owns(c)); @@ -507,7 +507,7 @@ public class AbstractQueuedSynchronizerT * Calling await without holding sync throws IllegalMonitorStateException */ public void testAwait_IllegalMonitor() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { c.await(); @@ -524,7 +524,7 @@ public class AbstractQueuedSynchronizerT * Calling signal without holding sync throws IllegalMonitorStateException */ public void testSignal_IllegalMonitor() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { c.signal(); @@ -541,7 +541,7 @@ public class AbstractQueuedSynchronizerT * awaitNanos without a signal times out */ public void testAwaitNanos_Timeout() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.acquire(1); @@ -558,7 +558,7 @@ public class AbstractQueuedSynchronizerT * Timed await without a signal times out */ public void testAwait_Timeout() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.acquire(1); @@ -574,7 +574,7 @@ public class AbstractQueuedSynchronizerT * awaitUntil without a signal times out */ public void testAwaitUntil_Timeout() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.acquire(1); @@ -591,9 +591,9 @@ public class AbstractQueuedSynchronizerT * await returns when signalled */ public void testAwait() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -734,9 +734,9 @@ public class AbstractQueuedSynchronizerT * getWaitingThreads throws IAE if not owned */ public void testGetWaitingThreadsIAE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); - final Mutex sync2 = new Mutex(); + final Mutex sync2 = new Mutex(); try { sync2.getWaitingThreads(c); shouldThrow(); @@ -750,7 +750,7 @@ public class AbstractQueuedSynchronizerT * getWaitingThreads throws IMSE if not synced */ public void testGetWaitingThreadsIMSE() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); try { sync.getWaitingThreads(c); @@ -767,9 +767,9 @@ public class AbstractQueuedSynchronizerT * hasWaiters returns true when a thread is waiting, else false */ public void testHasWaiters() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -809,9 +809,9 @@ public class AbstractQueuedSynchronizerT * getWaitQueueLength returns number of waiting threads */ public void testGetWaitQueueLength() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -826,7 +826,7 @@ public class AbstractQueuedSynchronizerT } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -870,9 +870,9 @@ public class AbstractQueuedSynchronizerT * getWaitingThreads returns only and all waiting threads */ public void testGetWaitingThreads() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -886,7 +886,7 @@ public class AbstractQueuedSynchronizerT } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -935,9 +935,9 @@ public class AbstractQueuedSynchronizerT * awaitUninterruptibly doesn't abort on interrupt */ public void testAwaitUninterruptibly() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { sync.acquire(1); c.awaitUninterruptibly(); @@ -964,9 +964,9 @@ public class AbstractQueuedSynchronizerT * await is interruptible */ public void testAwait_Interrupt() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -995,9 +995,9 @@ public class AbstractQueuedSynchronizerT * awaitNanos is interruptible */ public void testAwaitNanos_Interrupt() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -1026,9 +1026,9 @@ public class AbstractQueuedSynchronizerT * awaitUntil is interruptible */ public void testAwaitUntil_Interrupt() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t = new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -1058,9 +1058,9 @@ public class AbstractQueuedSynchronizerT * signalAll wakes up all threads */ public void testSignalAll() { - final Mutex sync = new Mutex(); + final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); - Thread t1 = new Thread(new Runnable() { + Thread t1 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -1073,7 +1073,7 @@ public class AbstractQueuedSynchronizerT } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -1191,7 +1191,7 @@ public class AbstractQueuedSynchronizerT unexpectedException(); } } - + /** * acquireSharedTimed returns after release @@ -1222,7 +1222,7 @@ public class AbstractQueuedSynchronizerT unexpectedException(); } } - + /** * acquireSharedInterruptibly throws IE if interrupted before released */ @@ -1257,7 +1257,7 @@ public class AbstractQueuedSynchronizerT try { threadAssertFalse(l.isSignalled()); l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); - threadShouldThrow(); + threadShouldThrow(); } catch(InterruptedException success){} } }); @@ -1297,5 +1297,5 @@ public class AbstractQueuedSynchronizerT } } - + }