Created
May 11, 2012 20:13
-
-
Save AlphaGit/2662168 to your computer and use it in GitHub Desktop.
Testing Threading and parallel tasks in C# 4.5
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
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