Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

parallel invoke and multithreading

Posted on 2013-02-07
2
Medium Priority
?
515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 1500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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