Differences between multithreaded and multitasking in C #

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?

+4
source share
2 answers

Tasks do not necessarily correspond to threads. They will be scheduled by the task library for threadpool threads much more efficiently than your thread code.

Threads are quite expensive to create. Tasks will queue and reuse streams as they appear, so when a stream is waiting for network I / O, it can be reused to perform another task. Threads that sit idle lose resources. You can only execute the number of threads corresponding to the processor core count (at the same time), so 100 threads mean that the context switches to all your cores at least 25 times each.

If you use tasks, just queue all 1000 email processing tasks, rather than modify them and allow them to copy. The task library will process the number of threads to run it.

+2
source

The quick answer of sorting is that the task is not equal to Thread. The task is queued in the task scheduler and then executed in the thread, but the order of 100 tasks does not mean that you will have 100 threads.

Typically, a task runs in a thread from a thread pool that has a finite size. When all these threads are busy, your tasks will have to wait until the thread becomes available for your task. You can also queue them for things like the user interface thread using the appropriate task scheduler, in which case they end up being run synchronously using the user interface thread's message loop (this is useful when interacting with the user interface).

In your example task, there is no need to actually limit the number of running tasks, since tasks will already be in the queue, and they will have to wait until the thread is available to start them. This is perhaps the best approach, because you allow the system to determine the maximum number of threads based on what the system can process, rather than assume that you have enough processor / memory to process it.

While your example using threads definitely means that you definitely allocate the number of threads that you allocate.

0
source

Source: https://habr.com/ru/post/1440132/


All Articles