Created
November 18, 2012 11:16
-
-
Save rrreese/4104604 to your computer and use it in GitHub Desktop.
A look at the Parallel class, .Net 4.0's new functionality to simplify multi-threading.
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
class ParallelTest | |
{ | |
class TestObject | |
{ | |
public string Name { get; set; } | |
public int Number { get; set; } | |
public override string ToString() | |
{ | |
return Name; | |
} | |
} | |
private readonly Random _random = new Random((int)DateTime.Now.Ticks); | |
readonly List<TestObject> _objects = new List<TestObject>(); | |
public ParallelTest() | |
{ | |
for (int i = 0; i < 10; i++) | |
_objects.Add(new TestObject {Name = generateRandomString(), Number = i}); | |
} | |
private static void writeList<T>(IEnumerable<T> t) | |
{ | |
foreach (var item in t) | |
{ | |
Console.WriteLine(item.ToString()); | |
} | |
Console.WriteLine("--------------"); | |
} | |
private string generateRandomString() | |
{ | |
const int stringLength = 10; | |
StringBuilder randomText = new StringBuilder(stringLength); | |
const int asciiA = 65; | |
const int asciiZ = 90; | |
const int range = asciiZ - asciiA; | |
for (int i = 0; i < stringLength; i++) | |
randomText.Append((char)(asciiA + _random.NextDouble() * range)); | |
return randomText.ToString(); | |
} | |
} |
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 void TestParallelFor() | |
{ | |
writeList(_objects); | |
Parallel.For(0, _objects.Count(), forMethod); | |
writeList(_objects); | |
Parallel.For(0, _objects.Count(), delegate(int i) | |
{ | |
_objects[i].Name = new string(_objects[i].Name.Reverse().ToArray()) + _objects[i].Number; | |
}); | |
writeList(_objects); | |
Parallel.For(0, _objects.Count(), i => | |
{ | |
_objects[i].Name = new string(_objects[i].Name.Reverse().ToArray()) + _objects[i].Number; | |
}); | |
writeList(_objects); | |
} | |
private void forMethod(int i) | |
{ | |
_objects[i].Name = new string(_objects[i].Name.Reverse().ToArray()) + _objects[i].Number; | |
} |
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 void TestParallelForEach() | |
{ | |
writeList(_objects); | |
Parallel.ForEach(_objects,forEachMethod); | |
writeList(_objects); | |
Parallel.ForEach(_objects, delegate(TestObject to) | |
{ | |
to.Name = new string(to.Name.Reverse().ToArray()) + to.Number; | |
}); | |
writeList(_objects); | |
Parallel.ForEach(_objects, to => | |
{ | |
to.Name = new string(to.Name.Reverse().ToArray()) + to.Number; | |
}); | |
writeList(_objects); | |
} | |
private static void forEachMethod(TestObject to) | |
{ | |
to.Name = new string(to.Name.Reverse().ToArray()) + to.Number; | |
} |
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 static void TestParallelInvoke() | |
{ | |
Parallel.Invoke(() => | |
{ | |
Thread.Sleep(1); | |
Console.Write("Hello"); | |
}, | |
delegate() | |
{ | |
Thread.Sleep(1000); | |
Console.Write("World"); | |
}, | |
invokeMethod); | |
} | |
private static void invokeMethod() | |
{ | |
Thread.Sleep(2000); | |
Console.Write("!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment