Solved

parallel invoke and multithreading

Posted on 2013-02-07
2
512 Views
Last Modified: 2013-02-07
Trying to figure out if parallel invoke and multithreading play perform similar functionality as the following two code :

Parallel :

Parallel.Invoke(() => FillDictionary(dict1, 1000000), () => FillDictionary(dict2, 1000000));


Multithreading :

var tasks = new Task[]
{
	Task.Factory.StartNew(
		() =>
		{
			Thread.Sleep(200);
			"Task 1 complete".Dump();
		}
	),
	Task.Factory.StartNew(
		() =>
		{
			Thread.Sleep(250);
			"Task 2 complete".Dump();
		}
	),
	Task.Factory.StartNew(
		() =>
		{
			Thread.Sleep(200);
			"Task 3 complete".Dump();
		}
	)
};

"Waiting for all tasks".Dump();
Task.WaitAny(tasks);
"Done waiting".Dump();

Open in new window


will both of the code run methods at once , or multithreading as in the code will sequentially run one after another?
0
Comment
Question by:doramail05
  • 2
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 38863064
Neither will definately run the tasks sequentially.

Parallel.Invoke will attempt to run the tasks concurrently (simultaneously - time slices to each)
Multithreading creates separate threads, one for each task that you give it and these will run concurrently.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38863076
Consider two tasks A and B.

Sequential code would be
A();
B();

In that you are certain that the code A is run completely before code B is even started.


When you use threads, one thread for A and one for B.
A might finish before B even starts.
B might finish before A.
You have no control over what is run when
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question