--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2006/02/24 00:03:16 1.20 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2009/11/16 04:57:09 1.23 @@ -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; @@ -73,7 +73,7 @@ public class AbstractQueuedSynchronizerT public void run() { try { sync.acquireInterruptibly(1); - } catch(InterruptedException success){} + } catch (InterruptedException success){} } } @@ -89,22 +89,22 @@ public class AbstractQueuedSynchronizerT try { sync.acquireInterruptibly(1); threadShouldThrow(); - } catch(InterruptedException success){} + } catch (InterruptedException success){} } } /** * 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)); @@ -146,15 +146,15 @@ public class AbstractQueuedSynchronizerT assertFalse(sync.hasQueuedThreads()); t1.join(); t2.join(); - } catch(Exception e){ + } 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)); @@ -192,15 +192,15 @@ public class AbstractQueuedSynchronizerT assertFalse(sync.isQueued(t2)); t1.join(); t2.join(); - } catch(Exception e){ + } 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)); @@ -222,16 +222,16 @@ public class AbstractQueuedSynchronizerT assertNull(sync.getFirstQueuedThread()); t1.join(); t2.join(); - } catch(Exception e){ + } 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)); @@ -252,15 +252,15 @@ public class AbstractQueuedSynchronizerT assertTrue(sync.hasContended()); t1.join(); t2.join(); - } catch(Exception e){ + } 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)); @@ -284,15 +284,15 @@ public class AbstractQueuedSynchronizerT assertTrue(sync.getQueuedThreads().isEmpty()); t1.join(); t2.join(); - } catch(Exception e){ + } 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)); @@ -316,15 +316,15 @@ public class AbstractQueuedSynchronizerT assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); t1.join(); t2.join(); - } catch(Exception e){ + } 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)); @@ -346,15 +346,15 @@ public class AbstractQueuedSynchronizerT assertTrue(sync.getSharedQueuedThreads().isEmpty()); t1.join(); t2.join(); - } catch(Exception e){ + } 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() { @@ -362,13 +362,13 @@ public class AbstractQueuedSynchronizerT try { sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); threadShouldThrow(); - } catch(InterruptedException success){} + } catch (InterruptedException success){} } }); try { t.start(); t.interrupt(); - } catch(Exception e){ + } catch (Exception e){ unexpectedException(); } } @@ -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() { @@ -389,15 +389,15 @@ public class AbstractQueuedSynchronizerT t.start(); t.join(); sync.release(1); - } catch(Exception e){ + } 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() { @@ -413,12 +413,12 @@ public class AbstractQueuedSynchronizerT t.start(); t.join(); sync.release(1); - } catch(Exception e){ + } catch (Exception e){ unexpectedException(); } - } - - + } + + /** * getState is true when acquired and false when not */ @@ -428,13 +428,13 @@ 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 { Thread.sleep(SMALL_DELAY_MS); } - catch(Exception e) { + catch (Exception e) { threadUnexpectedException(); } sync.release(1); @@ -446,7 +446,7 @@ public class AbstractQueuedSynchronizerT assertTrue(sync.isHeldExclusively()); t.join(); assertFalse(sync.isHeldExclusively()); - } catch(Exception e){ + } catch (Exception e){ unexpectedException(); } } @@ -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)); @@ -463,21 +463,22 @@ public class AbstractQueuedSynchronizerT t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); + Thread.sleep(SHORT_DELAY_MS); sync.release(1); t.join(); - } catch(Exception e){ + } 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) { + } catch (Exception e) { unexpectedException(); } Thread t = new Thread(new InterruptedSyncRunnable(sync)); @@ -486,7 +487,7 @@ public class AbstractQueuedSynchronizerT t.interrupt(); assertTrue(sync.isHeldExclusively()); t.join(); - } catch(Exception e){ + } catch (Exception e){ unexpectedException(); } } @@ -495,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)); @@ -506,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(); @@ -523,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(); @@ -540,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); @@ -557,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); @@ -573,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); @@ -590,16 +591,16 @@ 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); c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } @@ -733,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(); @@ -749,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); @@ -766,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); @@ -777,7 +778,7 @@ public class AbstractQueuedSynchronizerT c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } @@ -808,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); @@ -819,13 +820,13 @@ public class AbstractQueuedSynchronizerT c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -834,7 +835,7 @@ public class AbstractQueuedSynchronizerT c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } @@ -869,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); @@ -879,13 +880,13 @@ public class AbstractQueuedSynchronizerT c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); @@ -893,7 +894,7 @@ public class AbstractQueuedSynchronizerT c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } @@ -934,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(); @@ -963,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); @@ -973,7 +974,7 @@ public class AbstractQueuedSynchronizerT sync.release(1); threadShouldThrow(); } - catch(InterruptedException success) { + catch (InterruptedException success) { } } }); @@ -994,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); @@ -1004,7 +1005,7 @@ public class AbstractQueuedSynchronizerT sync.release(1); threadShouldThrow(); } - catch(InterruptedException success) { + catch (InterruptedException success) { } } }); @@ -1025,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); @@ -1036,7 +1037,7 @@ public class AbstractQueuedSynchronizerT sync.release(1); threadShouldThrow(); } - catch(InterruptedException success) { + catch (InterruptedException success) { } } }); @@ -1057,29 +1058,29 @@ 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); c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } }); - Thread t2 = new Thread(new Runnable() { + Thread t2 = new Thread(new Runnable() { public void run() { try { sync.acquire(1); c.await(); sync.release(1); } - catch(InterruptedException e) { + catch (InterruptedException e) { threadUnexpectedException(); } } @@ -1133,7 +1134,7 @@ public class AbstractQueuedSynchronizerT ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); Mutex r = (Mutex) in.readObject(); assertTrue(r.isHeldExclusively()); - } catch(Exception e){ + } catch (Exception e){ e.printStackTrace(); unexpectedException(); } @@ -1174,7 +1175,7 @@ public class AbstractQueuedSynchronizerT threadAssertFalse(l.isSignalled()); l.acquireSharedInterruptibly(0); threadAssertTrue(l.isSignalled()); - } catch(InterruptedException e){ + } catch (InterruptedException e){ threadUnexpectedException(); } } @@ -1190,7 +1191,7 @@ public class AbstractQueuedSynchronizerT unexpectedException(); } } - + /** * acquireSharedTimed returns after release @@ -1205,7 +1206,7 @@ public class AbstractQueuedSynchronizerT threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); threadAssertTrue(l.isSignalled()); - } catch(InterruptedException e){ + } catch (InterruptedException e){ threadUnexpectedException(); } } @@ -1221,7 +1222,7 @@ public class AbstractQueuedSynchronizerT unexpectedException(); } } - + /** * acquireSharedInterruptibly throws IE if interrupted before released */ @@ -1233,7 +1234,7 @@ public class AbstractQueuedSynchronizerT threadAssertFalse(l.isSignalled()); l.acquireSharedInterruptibly(0); threadShouldThrow(); - } catch(InterruptedException success){} + } catch (InterruptedException success){} } }); t.start(); @@ -1256,8 +1257,8 @@ public class AbstractQueuedSynchronizerT try { threadAssertFalse(l.isSignalled()); l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); - threadShouldThrow(); - } catch(InterruptedException success){} + threadShouldThrow(); + } catch (InterruptedException success){} } }); t.start(); @@ -1281,7 +1282,7 @@ public class AbstractQueuedSynchronizerT try { threadAssertFalse(l.isSignalled()); threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); - } catch(InterruptedException ie){ + } catch (InterruptedException ie){ threadUnexpectedException(); } } @@ -1296,5 +1297,5 @@ public class AbstractQueuedSynchronizerT } } - + }