コード例 #1
0
     public void TestGetWaitingThreads()
     {
         PublicReentrantReadWriteLock locker = new PublicReentrantReadWriteLock();
         Condition c = locker.WriteLock.NewCondition();
         Pair data = new Pair(locker, c);
         Thread t1 = new Thread(TestGetWaitingThreadsRun1);
         Thread t2 = new Thread(TestGetWaitingThreadsRun2);
 
         try
         {
             locker.WriteLock.Lock();
             Assert.IsTrue(locker.AccessGetWaitingThreads(c).IsEmpty());
             locker.WriteLock.UnLock();
             t1.Start(data);
             Thread.Sleep(SHORT_DELAY_MS);
             t2.Start(data);
             Thread.Sleep(SHORT_DELAY_MS);
             locker.WriteLock.Lock();
             Assert.IsTrue(locker.HasWaiters(c));
             Assert.IsTrue(locker.AccessGetWaitingThreads(c).Contains(t1));
             Assert.IsTrue(locker.AccessGetWaitingThreads(c).Contains(t2));
             c.SignalAll();
             locker.WriteLock.UnLock();
             Thread.Sleep(SHORT_DELAY_MS);
             locker.WriteLock.Lock();
             Assert.IsFalse(locker.HasWaiters(c));
             Assert.IsTrue(locker.AccessGetWaitingThreads(c).IsEmpty());
             locker.WriteLock.UnLock();
             t1.Join(SHORT_DELAY_MS);
             t2.Join(SHORT_DELAY_MS);
             Assert.IsFalse(t1.IsAlive);
             Assert.IsFalse(t2.IsAlive);
         }
         catch (Exception e)
         {
             UnexpectedException(e);
         }
     }
コード例 #2
0
 public void TestGetWaitingThreadsNRE()
 {
     PublicReentrantReadWriteLock locker = new PublicReentrantReadWriteLock();
     try
     {
         locker.AccessGetWaitingThreads(null);
         ShouldThrow();
     }
     catch (NullReferenceException)
     {
     }
     catch (Exception e)
     {
         UnexpectedException(e);
     }
 }
コード例 #3
0
 public void TestGetWaitingThreadsIMSE()
 {
     PublicReentrantReadWriteLock locker = new PublicReentrantReadWriteLock();
     Condition c = (locker.WriteLock.NewCondition());
     try
     {
         locker.AccessGetWaitingThreads(c);
         ShouldThrow();
     }
     catch (ThreadStateException)
     {
     }
     catch (Exception e)
     {
         UnexpectedException(e);
     }
 }
コード例 #4
0
        public void TestGetQueuedThreads()
        {
            PublicReentrantReadWriteLock locker = new PublicReentrantReadWriteLock();
            Thread t1 = new Thread(InterruptedLockRunnable);
            Thread t2 = new Thread(InterruptibleLockRunnable);

            try
            {
                Assert.IsTrue(locker.AccessQueuedThreads.IsEmpty());
                locker.WriteLock.Lock();
                Assert.IsTrue(locker.AccessQueuedThreads.IsEmpty());
                t1.Start(locker);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(locker.AccessQueuedThreads.Contains(t1));
                t2.Start(locker);
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(locker.AccessQueuedThreads.Contains(t1));
                Assert.IsTrue(locker.AccessQueuedThreads.Contains(t2));
                t1.Interrupt();
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsFalse(locker.AccessQueuedThreads.Contains(t1));
                Assert.IsTrue(locker.AccessQueuedThreads.Contains(t2));
                locker.WriteLock.UnLock();
                Thread.Sleep(SHORT_DELAY_MS);
                Assert.IsTrue(locker.AccessQueuedThreads.IsEmpty());
                t1.Join();
                t2.Join();
            }
            catch(Exception e)
            {
                UnexpectedException(e);
            }
        }