Skip to content

Instantly share code, notes, and snippets.

@olegchir
Created August 1, 2015 13:04
Show Gist options
  • Save olegchir/ee45b5920561be9a2bf2 to your computer and use it in GitHub Desktop.
Save olegchir/ee45b5920561be9a2bf2 to your computer and use it in GitHub Desktop.
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