--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2003/12/31 21:30:08 1.6 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2004/01/04 00:57:21 1.8 @@ -32,17 +32,17 @@ public class AbstractQueuedSynchronizerT private static class Sync extends AbstractQueuedSynchronizer { boolean isLocked() { return getState() == 1; } - public boolean tryAcquireExclusiveState(boolean isQueued, int acquires) { + public boolean tryAcquireExclusive(boolean isQueued, int acquires) { assert acquires == 1; // Does not use multiple acquires return compareAndSetState(0, 1); } - public boolean releaseExclusiveState(int releases) { + public boolean tryReleaseExclusive(int releases) { setState(0); return true; } - public void checkConditionAccess(Thread thread, boolean waiting) { + public void checkConditionAccess(Thread thread) { if (getState() == 0) throw new IllegalMonitorStateException(); } @@ -52,11 +52,12 @@ public class AbstractQueuedSynchronizerT s.defaultReadObject(); setState(0); // reset to unlocked state } + } - + private final Sync sync = new Sync(); public boolean tryLock() { - return sync.tryAcquireExclusiveState(false, 1); + return sync.tryAcquireExclusive(false, 1); } public void lock() { sync.acquireExclusiveUninterruptibly(1); @@ -71,6 +72,7 @@ public class AbstractQueuedSynchronizerT public Condition newCondition() { return sync.newCondition(); } public boolean isLocked() { return sync.isLocked(); } public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); } + public boolean hasContended() { return sync.hasContended(); } } /** @@ -147,6 +149,36 @@ public class AbstractQueuedSynchronizerT t1.join(); t2.join(); } catch(Exception e){ + unexpectedException(); + } + } + + + /** + * hasContended reports whether there has been contention + */ + public void testhasContended() { + final Mutex lock = new Mutex(); + Thread t1 = new Thread(new InterruptedLockRunnable(lock)); + Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); + try { + assertFalse(lock.hasContended()); + lock.lock(); + t1.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasContended()); + t2.start(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasContended()); + t1.interrupt(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasContended()); + lock.unlock(); + Thread.sleep(SHORT_DELAY_MS); + assertTrue(lock.hasContended()); + t1.join(); + t2.join(); + } catch(Exception e){ unexpectedException(); } }