public void ShouldNotDeadlock() { SimpleBlockingQueue _bq = new SimpleBlockingQueue(); const int THREAD_COUNT = 50; Thread[] putThreads = new Thread[THREAD_COUNT]; Thread[] getThreads = new Thread[THREAD_COUNT + (THREAD_COUNT / 2)]; Thread[] disableThreads = new Thread[THREAD_COUNT / 2]; for (int i = 0; i < getThreads.Length; i += 1) { getThreads[i] = new Thread(() => { _bq.Get(); _bq.Get(); }); getThreads[i].Start(); } for (int i = 0; i < putThreads.Length; i += 1) { var capture = i; putThreads[i] = new Thread(() => { _bq.Put(capture); _bq.Put(capture + putThreads.Length); }); putThreads[i].Start(); } for (int i = 0; i < disableThreads.Length; i += 1) { disableThreads[i] = new Thread(() => { _bq.Disable(); }); disableThreads[i].Start(); } for (int i = 0; i < putThreads.Length; i += 1) { putThreads[i].Join(); } for (int i = 0; i < getThreads.Length; i += 1) { getThreads[i].Join(); } for (int i = 0; i < disableThreads.Length; i += 1) { disableThreads[i].Join(); } // if we get here then there was no deadlock }
private static void Test() { const int THREAD_COUNT = 5; Thread[] putThreads = new Thread[THREAD_COUNT]; Thread[] getThreads = new Thread[THREAD_COUNT + (THREAD_COUNT / 2)]; Thread[] disableThreads = new Thread[THREAD_COUNT / 2]; for (int i = 0; i < getThreads.Length; i += 1) { getThreads[i] = new Thread(() => { _bq.Get(); _bq.Get(); }); getThreads[i].Start(); } for (int i = 0; i < putThreads.Length; i += 1) { var capture = i; putThreads[i] = new Thread(() => { _bq.Put(capture); _bq.Put(capture + putThreads.Length); }); putThreads[i].Start(); } for (int i = 0; i < disableThreads.Length; i += 1) { disableThreads[i] = new Thread(() => { _bq.Disable(); }); disableThreads[i].Start(); } for (int i = 0; i < putThreads.Length; i += 1) { putThreads[i].Join(); } for (int i = 0; i < getThreads.Length; i += 1) { getThreads[i].Join(); } for (int i = 0; i < disableThreads.Length; i += 1) { disableThreads[i].Join(); } Console.WriteLine("---"); Console.WriteLine("All threads completed without deadlocks."); }
public void ShouldReturnNullAfterDisabling() { SimpleBlockingQueue target = new SimpleBlockingQueue(); int testValue = 1; target.Put(testValue); var testValueBackBeforeDisable = target.Get(); Assert.AreEqual(testValue, testValueBackBeforeDisable); target.Put(testValue); target.Disable(); var testValueBackAfterDisable = target.Get(); Assert.AreEqual(null, testValueBackAfterDisable); }