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); }
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); }
internal ThreadPoolWorker(QueuedThreadPool queuedThreadPool) { _queuedThreadPool = queuedThreadPool; ThreadStart ts = new ThreadStart(Run); _thread = new System.Threading.Thread(ts); _thread.Priority = _queuedThreadPool._priority; _thread.IsBackground = _queuedThreadPool._background; }
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); } }
public void TestQueuedThreadPool() { //System.Threading.Thread.CurrentThread.Priority = ThreadPriority.Highest; QueuedThreadPool tp = new QueuedThreadPool(); tp.MinThreads = 5; tp.MaxThreads = 10; tp.MaxIdleTimeMs = 1000; tp.SpawnOrShrinkAt = 2; tp.ThreadsPriority = ThreadPriority.BelowNormal; tp.Start(); System.Threading.Thread.Sleep(500); Assert.AreEqual(5, tp.Threads); Assert.AreEqual(5, tp.IdleThreads); tp.Dispatch(_job); tp.Dispatch(_job); Assert.AreEqual(5, tp.Threads); //Assert.AreEqual(3,tp.IdleThreads); System.Threading.Thread.Sleep(5000); Assert.AreEqual(5, tp.Threads); Assert.AreEqual(5, tp.IdleThreads); for (int i = 0; i < 100; i++) { tp.Dispatch(_job); } Assert.Greater(tp.QueueSize, 10); Assert.LessOrEqual(tp.IdleThreads, 1); System.Threading.Thread.Sleep(2000); Assert.AreEqual(0, tp.QueueSize); Assert.Greater(tp.IdleThreads, 5); int threads = tp.Threads; Assert.Greater(threads, 5); System.Threading.Thread.Sleep(1500); Assert.Less(tp.Threads, threads); }
internal ThreadPoolWorker(QueuedThreadPool queuedThreadPool) { _queuedThreadPool = queuedThreadPool; ThreadStart ts = new ThreadStart(Run); _thread = new System.Threading.Thread(ts); _thread.Priority = _queuedThreadPool._priority; _thread.IsBackground = _queuedThreadPool._background; }