Skip to content

Instantly share code, notes, and snippets.

@AlphaGit
Created May 11, 2012 20:13
Show Gist options
  • Save AlphaGit/2662168 to your computer and use it in GitHub Desktop.
Save AlphaGit/2662168 to your computer and use it in GitHub Desktop.
Testing Threading and parallel tasks in C# 4.5
public class ThreadingClass
{
private static Random rnd = new Random();
public void StartSomeWork()
{
var threadNumber = 0;
var threadStart = new ThreadStart(() =>
{
Thread.CurrentThread.Name = string.Format("Thread {0}", ++threadNumber);
var myValue = rnd.Next(10);
Thread.Sleep(1000);
Console.WriteLine("{0} finished!", Thread.CurrentThread.Name);
});
var threadList = new List<Thread>();
for (var i = 1; i < 10; i++)
threadList.Add(new Thread(threadStart));
threadList.ForEach(t => t.Start());
threadList.ForEach(t => t.Join());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment