Possible duplicate:
What is the difference between task and thread?
I understand that the title itself can be duplicate, but I really read all the previous posts related to this topic, and still do not quite understand the behavior of the program.
I am currently writing a small program that checks 1000 email accounts. Undoubtedly, I believe that multithreading or multitasking is the right approach, since each thread / task is not an expensive computational process, but the duration of each thread is largely dependent on network I / O operations.
I think that in such a scenario it would also be wise to set the number of threads / tasks to a number that is much larger than the number of cores. (four for i5-750). So I set the number of threads or tasks to 100.
Code snippet written using the Task:
const int taskCount = 100; var tasks = new Task[taskCount]; var loopVal = (int) Math.Ceiling(1.0*EmailAddress.Count/taskCount); for (int i = 0; i < taskCount; i++) { var objContainer = new AutoCheck(i*loopVal, i*loopVal + loopVal); tasks[i] = new Task(objContainer.CheckMail); tasks[i].Start(); } Task.WaitAll(tasks);
The same piece of code written with Threads:
const int threadCount = 100; var threads = new Thread[threadCount]; var loopVal = (int)Math.Ceiling(1.0 * EmailAddress.Count / threadCount); for (int i = 0; i < threadCount; i++) { var objContainer = new AutoCheck(i * loopVal, i * loopVal + loopVal); threads[i] = new Thread(objContainer.CheckMail); threads[i].Start(); } foreach (Thread t in threads) t.Join(); runningTime.Stop(); Console.WriteLine(runningTime.Elapsed);
So what are the significant differences between the two?
source share