-
-
Save olegchir/ee45b5920561be9a2bf2 to your computer and use it in GitHub Desktop.
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.Collections.Generic; | |
using System.Threading; | |
namespace MaxNumberOfThreadsDemo | |
{ | |
public class Program | |
{ | |
private static List<Thread> _threads = new List<Thread>(); | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Creating Threads ..."); | |
try | |
{ | |
CreateThreadsWithDefaultStackSize(); | |
//CreateThreadsWithSmallerStackSize(); | |
//CreateThreadsWithSmallerThanMinStackSize(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex); | |
} | |
Console.ReadLine(); | |
Console.WriteLine("Cleanning up Threads ..."); | |
Cleanup(); | |
Console.WriteLine("Done"); | |
} | |
public static void CreateThreadsWithDefaultStackSize() | |
{ | |
for(int i=0; i< 10000; i++) | |
{ | |
Thread t = new Thread(DoWork); | |
_threads.Add(t); | |
t.Name = "Thread_" + i; | |
t.Start(); | |
Console.WriteLine(string.Format("{0} started!",t.Name)); | |
} | |
} | |
private static void Cleanup() | |
{ | |
foreach (var thread in _threads) | |
thread.Abort(); | |
_threads.Clear(); | |
_threads = null; | |
} | |
private static void DoWork() | |
{ | |
Thread.Sleep(Int32.MaxValue-1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment