Last active
December 19, 2015 21:19
-
-
Save lurongkai/6019656 to your computer and use it in GitHub Desktop.
ThreadPoolHittingTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Collections.Generic; | |
using System.Collections.Concurrent; | |
namespace ThreadPoolHittingTest | |
{ | |
class MainClass | |
{ | |
private static ConcurrentDictionary<int, Thread> ThreadDict = new ConcurrentDictionary<int, Thread>(); | |
public static void Main(string[] args) { | |
ThreadPool.SetMaxThreads(20, 10); | |
for (int num = 0; num < 10000; num ++) { | |
ThreadPool.QueueUserWorkItem(DoWork, num); | |
} | |
Console.ReadLine(); | |
} | |
static void DoWork(object state) { | |
Thread.Sleep(100); | |
var thread = Thread.CurrentThread; | |
if (ThreadDict.ContainsKey(thread.ManagedThreadId)) { | |
CompareThread(ThreadDict[thread.ManagedThreadId], thread); | |
} else { | |
ThreadDict.TryAdd(thread.ManagedThreadId, thread); | |
} | |
} | |
static void CompareThread(Thread thread1, Thread thread2) { | |
var referenceEqual = object.ReferenceEquals(thread1, thread2); | |
if (referenceEqual) { | |
Console.WriteLine("{0} reused.", thread1.ManagedThreadId); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment