Skip to content

Instantly share code, notes, and snippets.

@lurongkai
Last active December 19, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lurongkai/6019656 to your computer and use it in GitHub Desktop.
Save lurongkai/6019656 to your computer and use it in GitHub Desktop.
ThreadPoolHittingTest
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