static void RunThreadPool() { QueuedThreadPool tp = new QueuedThreadPool(); tp.MaxStopTimeMs = 500; tp.Start(); // dispatch jobs for (int i = 0; i < 100; i++) { tp.Dispatch(new ThreadStart( () => { Log.Info("Running job with thread id " + System.Threading.Thread.CurrentThread.Name); //while (true) //{ try { System.Threading.Thread.Sleep(1000); } catch (ThreadInterruptedException) { } //} } )); } System.Threading.Thread.Sleep(1000); long beforeStop = (DateTime.UtcNow.Ticks/1000); tp.Stop(); long afterStop = (DateTime.UtcNow.Ticks/1000); Log.Info("Time to Stop {0}", afterStop - beforeStop); }
public void TestMaxStopTime() { QueuedThreadPool tp = new QueuedThreadPool(); tp.MinThreads = 500; tp.MaxThreads = 1000; tp.MaxStopTimeMs = 500; tp.Start(); // dispatch jobs for (int i = 0; i < 5000; i++) { tp.Dispatch(new ThreadStart( () => { //Log.Info("Running job with thread id " + System.Threading.Thread.CurrentThread.Name); while (true) { try { System.Threading.Thread.Sleep(1000); } catch (ThreadInterruptedException){} } } )); } System.Threading.Thread.Sleep(100); long beforeStop = (DateTime.UtcNow.Ticks/1000); tp.Stop(); long afterStop = (DateTime.UtcNow.Ticks/1000); Assert.IsTrue(tp.IsStopped); Assert.Less(afterStop - beforeStop, 100000); }
public void TestStress() { QueuedThreadPool tp = new QueuedThreadPool(); tp.MinThreads = 240; tp.MaxThreads = 250; tp.MaxIdleTimeMs = 100; tp.Start(); tp.MinThreads = 90; int count = 0; Random random = new Random((int)(DateTime.UtcNow.Ticks/1000)); int loops = 16000; try { for (int i = 0; i < loops; ) { int burst = random.Next(100); for (int b = 0; b < burst && i < loops; b++) { if (i % 20 == 0) Console.Error.Write('.'); if (i % 1600 == 1599) Console.Error.WriteLine(); if (i == 1000) tp.MinThreads = 10; if (i == 10000) tp.MaxThreads = 20; i++; tp.Dispatch( new ThreadStart( () => { int s = random.Next(50); try { System.Threading.Thread.Sleep(s); } catch (ThreadInterruptedException e) { Console.WriteLine(e.StackTrace); } finally { Interlocked.Increment(ref count); } } ) ); } System.Threading.Thread.Sleep(random.Next(100)); } System.Threading.Thread.Sleep(2000); tp.Stop(); Assert.AreEqual(loops, count); } catch (Exception e) { //Console.Error.WriteLine(e.StackTrace); Assert.IsTrue(false, e.Message); } }