Methods Summary |
---|
public static void | main(java.lang.String[] args)
junit.textui.TestRunner.run (suite());
|
public static junit.framework.Test | suite()
return new TestSuite(AbstractQueuedSynchronizerTest.class);
|
public void | testAcquire()acquiring released sync succeeds
Mutex rl = new Mutex();
rl.acquire(1);
assertTrue(rl.isHeldExclusively());
rl.release(1);
assertFalse(rl.isHeldExclusively());
|
public void | testAcquireInterruptibly1()acquireInterruptibly is interruptible.
final Mutex sync = new Mutex();
sync.acquire(1);
Thread t = new Thread(new InterruptedSyncRunnable(sync));
try {
t.start();
t.interrupt();
sync.release(1);
t.join();
} catch(Exception e){
unexpectedException();
}
|
public void | testAcquireInterruptibly2()acquireInterruptibly succeeds when released, else is interruptible
final Mutex sync = new Mutex();
try {
sync.acquireInterruptibly(1);
} catch(Exception e) {
unexpectedException();
}
Thread t = new Thread(new InterruptedSyncRunnable(sync));
try {
t.start();
t.interrupt();
assertTrue(sync.isHeldExclusively());
t.join();
} catch(Exception e){
unexpectedException();
}
|
public void | testAcquireNanos_Timeout()tryAcquireNanos on an exclusively held sync times out
final Mutex sync = new Mutex();
sync.acquire(1);
Thread t = new Thread(new Runnable() {
public void run() {
try {
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
} catch (Exception ex) {
threadUnexpectedException();
}
}
});
try {
t.start();
t.join();
sync.release(1);
} catch(Exception e){
unexpectedException();
}
|
public void | testAcquireSharedInterruptibly()acquireSharedInterruptibly returns after release, but not before
final BooleanLatch l = new BooleanLatch();
Thread t = new Thread(new Runnable() {
public void run() {
try {
threadAssertFalse(l.isSignalled());
l.acquireSharedInterruptibly(0);
threadAssertTrue(l.isSignalled());
} catch(InterruptedException e){
threadUnexpectedException();
}
}
});
try {
t.start();
assertFalse(l.isSignalled());
Thread.sleep(SHORT_DELAY_MS);
l.releaseShared(0);
assertTrue(l.isSignalled());
t.join();
} catch (InterruptedException e){
unexpectedException();
}
|
public void | testAcquireSharedInterruptibly_InterruptedException()acquireSharedInterruptibly throws IE if interrupted before released
final BooleanLatch l = new BooleanLatch();
Thread t = new Thread(new Runnable() {
public void run() {
try {
threadAssertFalse(l.isSignalled());
l.acquireSharedInterruptibly(0);
threadShouldThrow();
} catch(InterruptedException success){}
}
});
t.start();
try {
assertFalse(l.isSignalled());
t.interrupt();
t.join();
} catch (InterruptedException e){
unexpectedException();
}
|
public void | testAcquireSharedNanos_InterruptedException()acquireSharedTimed throws IE if interrupted before released
final BooleanLatch l = new BooleanLatch();
Thread t = new Thread(new Runnable() {
public void run() {
try {
threadAssertFalse(l.isSignalled());
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
threadShouldThrow();
} catch(InterruptedException success){}
}
});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
assertFalse(l.isSignalled());
t.interrupt();
t.join();
} catch (InterruptedException e){
unexpectedException();
}
|
public void | testAcquireSharedNanos_Timeout()acquireSharedTimed times out if not released before timeout
final BooleanLatch l = new BooleanLatch();
Thread t = new Thread(new Runnable() {
public void run() {
try {
threadAssertFalse(l.isSignalled());
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
} catch(InterruptedException ie){
threadUnexpectedException();
}
}
});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
assertFalse(l.isSignalled());
t.join();
} catch (InterruptedException e){
unexpectedException();
}
|
public void | testAsquireSharedTimed()acquireSharedTimed returns after release
final BooleanLatch l = new BooleanLatch();
Thread t = new Thread(new Runnable() {
public void run() {
try {
threadAssertFalse(l.isSignalled());
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
threadAssertTrue(l.isSignalled());
} catch(InterruptedException e){
threadUnexpectedException();
}
}
});
try {
t.start();
assertFalse(l.isSignalled());
Thread.sleep(SHORT_DELAY_MS);
l.releaseShared(0);
assertTrue(l.isSignalled());
t.join();
} catch (InterruptedException e){
unexpectedException();
}
|
public void | testAwait()await returns when signalled
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
Thread t = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
c.await();
sync.release(1);
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
sync.acquire(1);
c.signal();
sync.release(1);
t.join(SHORT_DELAY_MS);
assertFalse(t.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testAwaitNanos_Interrupt()awaitNanos is interruptible
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
Thread t = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
c.awaitNanos(1000 * 1000 * 1000); // 1 sec
sync.release(1);
threadShouldThrow();
}
catch(InterruptedException success) {
}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join(SHORT_DELAY_MS);
assertFalse(t.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testAwaitNanos_Timeout()awaitNanos without a signal times out
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
try {
sync.acquire(1);
long t = c.awaitNanos(100);
assertTrue(t <= 0);
sync.release(1);
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testAwaitUninterruptibly()awaitUninterruptibly doesn't abort on interrupt
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
Thread t = new Thread(new Runnable() {
public void run() {
sync.acquire(1);
c.awaitUninterruptibly();
sync.release(1);
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
sync.acquire(1);
c.signal();
sync.release(1);
assert(t.isInterrupted());
t.join(SHORT_DELAY_MS);
assertFalse(t.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testAwaitUntil_Interrupt()awaitUntil is interruptible
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
Thread t = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
java.util.Date d = new java.util.Date();
c.awaitUntil(new java.util.Date(d.getTime() + 10000));
sync.release(1);
threadShouldThrow();
}
catch(InterruptedException success) {
}
}
});
try {
t.start();
// BEGIN android-changed
Thread.sleep(SMALL_DELAY_MS); // SHORT_DELAY_MS was flaky on Android
t.interrupt();
t.join(SMALL_DELAY_MS); // SHORT_DELAY_MS was flaky on Android
// END android-changed
assertFalse(t.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testAwaitUntil_Timeout()awaitUntil without a signal times out
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
try {
sync.acquire(1);
java.util.Date d = new java.util.Date();
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
sync.release(1);
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testAwait_IllegalMonitor()Calling await without holding sync throws IllegalMonitorStateException
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
try {
c.await();
shouldThrow();
}
catch (IllegalMonitorStateException success) {
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testAwait_Interrupt()await is interruptible
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
Thread t = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
c.await();
sync.release(1);
threadShouldThrow();
}
catch(InterruptedException success) {
}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join(SHORT_DELAY_MS);
assertFalse(t.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testAwait_Timeout()Timed await without a signal times out
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
try {
sync.acquire(1);
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
sync.release(1);
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testGetExclusiveQueuedThreads()getExclusiveQueuedThreads includes waiting threads
final Mutex sync = new Mutex();
Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
try {
assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
sync.acquire(1);
assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
t1.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
t2.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
t1.interrupt();
Thread.sleep(SHORT_DELAY_MS);
assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
sync.release(1);
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
t1.join();
t2.join();
} catch(Exception e){
unexpectedException();
}
|
public void | testGetFirstQueuedThread()getFirstQueuedThread returns first waiting thread or null if none
final Mutex sync = new Mutex();
Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
try {
assertNull(sync.getFirstQueuedThread());
sync.acquire(1);
t1.start();
Thread.sleep(SHORT_DELAY_MS);
assertEquals(t1, sync.getFirstQueuedThread());
t2.start();
Thread.sleep(SHORT_DELAY_MS);
assertEquals(t1, sync.getFirstQueuedThread());
t1.interrupt();
Thread.sleep(SHORT_DELAY_MS);
Thread.sleep(SHORT_DELAY_MS);
assertEquals(t2, sync.getFirstQueuedThread());
sync.release(1);
Thread.sleep(SHORT_DELAY_MS);
assertNull(sync.getFirstQueuedThread());
t1.join();
t2.join();
} catch(Exception e){
unexpectedException();
}
|
public void | testGetQueuedThreads()getQueuedThreads includes waiting threads
final Mutex sync = new Mutex();
Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
try {
assertTrue(sync.getQueuedThreads().isEmpty());
sync.acquire(1);
assertTrue(sync.getQueuedThreads().isEmpty());
t1.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.getQueuedThreads().contains(t1));
t2.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.getQueuedThreads().contains(t1));
assertTrue(sync.getQueuedThreads().contains(t2));
t1.interrupt();
Thread.sleep(SHORT_DELAY_MS);
assertFalse(sync.getQueuedThreads().contains(t1));
assertTrue(sync.getQueuedThreads().contains(t2));
sync.release(1);
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.getQueuedThreads().isEmpty());
t1.join();
t2.join();
} catch(Exception e){
unexpectedException();
}
|
public void | testGetSharedQueuedThreads()getSharedQueuedThreads does not include exclusively waiting threads
final Mutex sync = new Mutex();
Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
try {
assertTrue(sync.getSharedQueuedThreads().isEmpty());
sync.acquire(1);
assertTrue(sync.getSharedQueuedThreads().isEmpty());
t1.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.getSharedQueuedThreads().isEmpty());
t2.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.getSharedQueuedThreads().isEmpty());
t1.interrupt();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.getSharedQueuedThreads().isEmpty());
sync.release(1);
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.getSharedQueuedThreads().isEmpty());
t1.join();
t2.join();
} catch(Exception e){
unexpectedException();
}
|
public void | testGetState()getState is true when acquired and false when not
final Mutex sync = new Mutex();
sync.acquire(1);
assertTrue(sync.isHeldExclusively());
sync.release(1);
assertFalse(sync.isHeldExclusively());
Thread t = new Thread(new Runnable() {
public void run() {
sync.acquire(1);
try {
Thread.sleep(SMALL_DELAY_MS);
}
catch(Exception e) {
threadUnexpectedException();
}
sync.release(1);
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.isHeldExclusively());
t.join();
assertFalse(sync.isHeldExclusively());
} catch(Exception e){
unexpectedException();
}
|
public void | testGetStateWithReleaseShared()tryReleaseShared setting state changes getState
final BooleanLatch l = new BooleanLatch();
assertFalse(l.isSignalled());
l.releaseShared(0);
assertTrue(l.isSignalled());
|
public void | testGetWaitQueueLength()getWaitQueueLength returns number of waiting threads
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
threadAssertFalse(sync.hasWaiters(c));
threadAssertEquals(0, sync.getWaitQueueLength(c));
c.await();
sync.release(1);
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
threadAssertTrue(sync.hasWaiters(c));
threadAssertEquals(1, sync.getWaitQueueLength(c));
c.await();
sync.release(1);
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
try {
t1.start();
Thread.sleep(SHORT_DELAY_MS);
t2.start();
Thread.sleep(SHORT_DELAY_MS);
sync.acquire(1);
assertTrue(sync.hasWaiters(c));
assertEquals(2, sync.getWaitQueueLength(c));
c.signalAll();
sync.release(1);
Thread.sleep(SHORT_DELAY_MS);
sync.acquire(1);
assertFalse(sync.hasWaiters(c));
assertEquals(0, sync.getWaitQueueLength(c));
sync.release(1);
t1.join(SHORT_DELAY_MS);
t2.join(SHORT_DELAY_MS);
assertFalse(t1.isAlive());
assertFalse(t2.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testGetWaitQueueLengthIAE()getWaitQueueLength throws IAE if not owned
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
final Mutex sync2 = new Mutex();
try {
sync2.getWaitQueueLength(c);
shouldThrow();
} catch (IllegalArgumentException success) {
} catch (Exception ex) {
unexpectedException();
}
|
public void | testGetWaitQueueLengthIMSE()getWaitQueueLength throws IMSE if not synced
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
try {
sync.getWaitQueueLength(c);
shouldThrow();
} catch (IllegalMonitorStateException success) {
} catch (Exception ex) {
unexpectedException();
}
|
public void | testGetWaitQueueLengthNPE()getWaitQueueLength throws NPE if null
final Mutex sync = new Mutex();
try {
sync.getWaitQueueLength(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (Exception ex) {
unexpectedException();
}
|
public void | testGetWaitingThreads()getWaitingThreads returns only and all waiting threads
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
c.await();
sync.release(1);
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
c.await();
sync.release(1);
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
try {
sync.acquire(1);
assertTrue(sync.getWaitingThreads(c).isEmpty());
sync.release(1);
t1.start();
Thread.sleep(SHORT_DELAY_MS);
t2.start();
Thread.sleep(SHORT_DELAY_MS);
sync.acquire(1);
assertTrue(sync.hasWaiters(c));
assertTrue(sync.getWaitingThreads(c).contains(t1));
assertTrue(sync.getWaitingThreads(c).contains(t2));
c.signalAll();
sync.release(1);
Thread.sleep(SHORT_DELAY_MS);
sync.acquire(1);
assertFalse(sync.hasWaiters(c));
assertTrue(sync.getWaitingThreads(c).isEmpty());
sync.release(1);
t1.join(SHORT_DELAY_MS);
t2.join(SHORT_DELAY_MS);
assertFalse(t1.isAlive());
assertFalse(t2.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testGetWaitingThreadsIAE()getWaitingThreads throws IAE if not owned
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
final Mutex sync2 = new Mutex();
try {
sync2.getWaitingThreads(c);
shouldThrow();
} catch (IllegalArgumentException success) {
} catch (Exception ex) {
unexpectedException();
}
|
public void | testGetWaitingThreadsIMSE()getWaitingThreads throws IMSE if not synced
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
try {
sync.getWaitingThreads(c);
shouldThrow();
} catch (IllegalMonitorStateException success) {
} catch (Exception ex) {
unexpectedException();
}
|
public void | testGetWaitingThreadsNPE()getWaitingThreads throws NPE if null
final Mutex sync = new Mutex();
try {
sync.getWaitingThreads(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (Exception ex) {
unexpectedException();
}
|
public void | testHasContended()hasContended reports false if no thread has ever blocked, else true
final Mutex sync = new Mutex();
Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
try {
assertFalse(sync.hasContended());
sync.acquire(1);
t1.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.hasContended());
t2.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.hasContended());
t1.interrupt();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.hasContended());
sync.release(1);
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.hasContended());
t1.join();
t2.join();
} catch(Exception e){
unexpectedException();
}
|
public void | testHasWaiters()hasWaiters returns true when a thread is waiting, else false
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
Thread t = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
threadAssertFalse(sync.hasWaiters(c));
threadAssertEquals(0, sync.getWaitQueueLength(c));
c.await();
sync.release(1);
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
sync.acquire(1);
assertTrue(sync.hasWaiters(c));
assertEquals(1, sync.getWaitQueueLength(c));
c.signal();
sync.release(1);
Thread.sleep(SHORT_DELAY_MS);
sync.acquire(1);
assertFalse(sync.hasWaiters(c));
assertEquals(0, sync.getWaitQueueLength(c));
sync.release(1);
t.join(SHORT_DELAY_MS);
assertFalse(t.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testHasWaitersIAE()hasWaiters throws IAE if not owned
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
final Mutex sync2 = new Mutex();
try {
sync2.hasWaiters(c);
shouldThrow();
} catch (IllegalArgumentException success) {
} catch (Exception ex) {
unexpectedException();
}
|
public void | testHasWaitersIMSE()hasWaiters throws IMSE if not synced
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
try {
sync.hasWaiters(c);
shouldThrow();
} catch (IllegalMonitorStateException success) {
} catch (Exception ex) {
unexpectedException();
}
|
public void | testHasWaitersNPE()hasWaiters throws NPE if null
final Mutex sync = new Mutex();
try {
sync.hasWaiters(null);
shouldThrow();
} catch (NullPointerException success) {
} catch (Exception ex) {
unexpectedException();
}
|
public void | testInterruptedException2()tryAcquireNanos is interruptible.
final Mutex sync = new Mutex();
sync.acquire(1);
Thread t = new Thread(new Runnable() {
public void run() {
try {
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
threadShouldThrow();
} catch(InterruptedException success){}
}
});
try {
t.start();
t.interrupt();
} catch(Exception e){
unexpectedException();
}
|
public void | testIsHeldExclusively()isHeldExclusively is false upon construction
Mutex rl = new Mutex();
assertFalse(rl.isHeldExclusively());
|
public void | testIsQueued()isQueued reports whether a thread is queued.
final Mutex sync = new Mutex();
Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
try {
assertFalse(sync.isQueued(t1));
assertFalse(sync.isQueued(t2));
sync.acquire(1);
t1.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.isQueued(t1));
t2.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.isQueued(t1));
assertTrue(sync.isQueued(t2));
t1.interrupt();
Thread.sleep(SHORT_DELAY_MS);
assertFalse(sync.isQueued(t1));
assertTrue(sync.isQueued(t2));
sync.release(1);
Thread.sleep(SHORT_DELAY_MS);
assertFalse(sync.isQueued(t1));
Thread.sleep(SHORT_DELAY_MS);
assertFalse(sync.isQueued(t2));
t1.join();
t2.join();
} catch(Exception e){
unexpectedException();
}
|
public void | testIsQueuedNPE()isQueued(null) throws NPE
final Mutex sync = new Mutex();
try {
sync.isQueued(null);
shouldThrow();
} catch (NullPointerException success) {
}
|
public void | testOwns()owns is true for a condition created by sync else false
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
final Mutex sync2 = new Mutex();
assertTrue(sync.owns(c));
assertFalse(sync2.owns(c));
|
public void | testReleaseShared()releaseShared has no effect when already signalled
final BooleanLatch l = new BooleanLatch();
assertFalse(l.isSignalled());
l.releaseShared(0);
assertTrue(l.isSignalled());
l.releaseShared(0);
assertTrue(l.isSignalled());
|
public void | testSerialization()A serialized AQS deserializes with current state
Mutex l = new Mutex();
l.acquire(1);
assertTrue(l.isHeldExclusively());
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
out.writeObject(l);
out.close();
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
Mutex r = (Mutex) in.readObject();
assertTrue(r.isHeldExclusively());
} catch(Exception e){
e.printStackTrace();
unexpectedException();
}
|
public void | testSignalAll()signalAll wakes up all threads
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
c.await();
sync.release(1);
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
sync.acquire(1);
c.await();
sync.release(1);
}
catch(InterruptedException e) {
threadUnexpectedException();
}
}
});
try {
t1.start();
t2.start();
Thread.sleep(SHORT_DELAY_MS);
sync.acquire(1);
c.signalAll();
sync.release(1);
t1.join(SHORT_DELAY_MS);
t2.join(SHORT_DELAY_MS);
assertFalse(t1.isAlive());
assertFalse(t2.isAlive());
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testSignal_IllegalMonitor()Calling signal without holding sync throws IllegalMonitorStateException
final Mutex sync = new Mutex();
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
try {
c.signal();
shouldThrow();
}
catch (IllegalMonitorStateException success) {
}
catch (Exception ex) {
unexpectedException();
}
|
public void | testToString()toString indicates current state
Mutex sync = new Mutex();
String us = sync.toString();
assertTrue(us.indexOf("State = 0") >= 0);
sync.acquire(1);
String ls = sync.toString();
assertTrue(ls.indexOf("State = 1") >= 0);
|
public void | testTryAcquire()tryAcquire on an released sync succeeds
Mutex rl = new Mutex();
assertTrue(rl.tryAcquire(1));
assertTrue(rl.isHeldExclusively());
rl.release(1);
|
public void | testTryAcquireWhenSynced()TryAcquire on exclusively held sync fails
final Mutex sync = new Mutex();
sync.acquire(1);
Thread t = new Thread(new Runnable() {
public void run() {
threadAssertFalse(sync.tryAcquire(1));
}
});
try {
t.start();
t.join();
sync.release(1);
} catch(Exception e){
unexpectedException();
}
|
public void | testhasQueuedThreads()hasQueuedThreads reports whether there are waiting threads
final Mutex sync = new Mutex();
Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
try {
assertFalse(sync.hasQueuedThreads());
sync.acquire(1);
t1.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.hasQueuedThreads());
t2.start();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.hasQueuedThreads());
t1.interrupt();
Thread.sleep(SHORT_DELAY_MS);
assertTrue(sync.hasQueuedThreads());
sync.release(1);
Thread.sleep(SHORT_DELAY_MS);
assertFalse(sync.hasQueuedThreads());
t1.join();
t2.join();
} catch(Exception e){
unexpectedException();
}
|