public void TestGetQueuedThreads() { PublicReentrantLock locker = new PublicReentrantLock(); Thread t1 = new Thread(InterruptedLockRunnable); Thread t2 = new Thread(InterruptibleLockRunnable); try { Assert.IsTrue(locker.GetAtQueuedThreads().IsEmpty()); locker.Lock(); Assert.IsTrue(locker.GetAtQueuedThreads().IsEmpty()); t1.Start(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(locker.GetAtQueuedThreads().Contains(t1)); t2.Start(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(locker.GetAtQueuedThreads().Contains(t1)); Assert.IsTrue(locker.GetAtQueuedThreads().Contains(t2)); t1.Interrupt(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsFalse(locker.GetAtQueuedThreads().Contains(t1)); Assert.IsTrue(locker.GetAtQueuedThreads().Contains(t2)); locker.UnLock(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(locker.GetAtQueuedThreads().IsEmpty()); t1.Join(); t2.Join(); } catch (Exception e) { UnexpectedException(e); } }
public void TestGetWaitingThreads() { PublicReentrantLock locker = new PublicReentrantLock(); Condition c = locker.NewCondition(); Pair data = new Pair(locker, c); Thread t1 = new Thread(TestGetWaitingThreadsRunnable1); Thread t2 = new Thread(TestGetWaitingThreadsRunnable2); try { locker.Lock(); Assert.IsTrue(locker.GetWaitingThreads(c).IsEmpty()); locker.UnLock(); t1.Start(data); Thread.Sleep(SHORT_DELAY_MS); t2.Start(data); Thread.Sleep(SHORT_DELAY_MS); locker.Lock(); Assert.IsTrue(locker.HasWaiters(c)); Assert.IsTrue(locker.GetWaitingThreads(c).Contains(t1)); Assert.IsTrue(locker.GetWaitingThreads(c).Contains(t2)); c.SignalAll(); locker.UnLock(); Thread.Sleep(SHORT_DELAY_MS); locker.Lock(); Assert.IsFalse(locker.HasWaiters(c)); Assert.IsTrue(locker.GetWaitingThreads(c).IsEmpty()); locker.UnLock(); t1.Join(SHORT_DELAY_MS); t2.Join(SHORT_DELAY_MS); Assert.IsFalse(t1.IsAlive); Assert.IsFalse(t2.IsAlive); } catch (Exception e) { UnexpectedException(e); } }
public void TestGetQueuedThreads() { PublicReentrantLock locker = new PublicReentrantLock(); Thread t1 = new Thread(InterruptedLockRunnable); Thread t2 = new Thread(InterruptibleLockRunnable); try { Assert.IsTrue(locker.GetAtQueuedThreads().IsEmpty()); locker.Lock(); Assert.IsTrue(locker.GetAtQueuedThreads().IsEmpty()); t1.Start(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(locker.GetAtQueuedThreads().Contains(t1)); t2.Start(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(locker.GetAtQueuedThreads().Contains(t1)); Assert.IsTrue(locker.GetAtQueuedThreads().Contains(t2)); t1.Interrupt(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsFalse(locker.GetAtQueuedThreads().Contains(t1)); Assert.IsTrue(locker.GetAtQueuedThreads().Contains(t2)); locker.UnLock(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(locker.GetAtQueuedThreads().IsEmpty()); t1.Join(); t2.Join(); } catch(Exception e) { UnexpectedException(e); } }