Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

parallel invoke and multithreading

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
doramail05
Asked:
doramail05
  • 2
1 Solution
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now