Solved

parallel invoke and multithreading

Posted on 2013-02-07
2
511 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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

778 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