Just for knowledge purposes, how can I verify that n methods are executing in parallel?
Parallel.For(0, 10, i => DoSomething(i));
...
void DoSomething(int par)
{
Console.WriteLine($"Test: {par}");
}
Is there a test that can assure me that DoSomething isn't executed sequentially (If Console.WriteLine
isn't a good test, what could be a very simple test function?)
In your example the methods will definitely be sequential because the .NET console will synchronize calls from different threads.
You can, however, check the thread ID that is running the method using Thread.ManagedThreadID or Environment.CurrentManagedThreadId (for .NET 4.5)
class Program
{
static void Main(string[] args)
{
// first run with default number of threads
Parallel.For(0, 10, i => DoSomething(i));
Console.ReadLine();
// now run with fewer threads...
Parallel.For(0, 10, new ParallelOptions{
MaxDegreeOfParallelism = 2
}, i => DoSomething(i));
Console.ReadLine();
}
static void DoSomething(int par)
{
int i = Environment.CurrentManagedThreadId;
// int i = Thread.ManagedThreadId; -- .NET 4.0 and under
Thread.Sleep(200);
Console.WriteLine("Test: "+ par.ToString() +
", Thread :" + i.ToString());
}
}
If you run this without Thread.Sleep
you'll notice that only a few threads will be used since the call will complete quickly enough for the thread to be returned to the pool in time to pick up the next job being distributed by the queue.
Adding the sleep will delay the thread (simulating work) long enough that the threadpool will have to source more threads to get the job spun out in a timely manner.
When running any task in parallel it is easy to check that the tasks are, in fact, all running in separate threads. Whether or not you get a performance benefit, however, will depend on how efficiently those threads can work. As you've seen, some operations invoke contention on a shared resource (like writing to the console) and can prevent your many threads from operating simultaneously.
Other sources of contention can be, for example, memory allocation - if your threads are all heavily allocating and de-allocating memory then they can end up spending a lot of time waiting for their turn with the memory manager. This will limit the actual degree of parallelism that you will achieve.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With