Difference between Task (System.Threading.Task) an

2019-01-13 13:36发布

问题:

From what I understand about the difference between Task & Thread is that task happened in the thread-pool while the thread is something that I need to managed by myself .. ( and that task can be cancel and return to the thread-pool in the end of his mission )

But in some blog I read that if the operating system need to create task and create thread => it will be easier to create ( and destroy ) task.

Someone can explain please why creating task is simple that thread ?

( or maybe I missing something here ... )

回答1:

I think that what you are talking about when you say Task is a System.Threading.Task. If that's the case then you can think about it this way:

  • A program can have many threads, but a processor core can only run one Thread at a time.
    • Threads are very expensive, and switching between the threads that are running is also very expensive.
    • So... Having thousands of threads doing stuff is inefficient. Imagine if your teacher gave you 10,000 tasks to do. You'd spend so much time cycling between them that you'd never get anything done. The same thing can happen to the CPU if you start too many threads.

To get around this, the .NET framework allows you to create Tasks. Tasks are a bit of work bundled up into an object, and they allow you to do interesting things like capture the output of that work and chain pieces of work together (first go to the store, then buy a magazine).

Tasks are scheduled on a pool of threads. The specific number of threads depends on the scheduler used, but the default scheduler tries to pick a number of threads that is optimal for the number of CPU cores that you have and how much time your tasks are spending actually using CPU time. If you want to, you can even write your own scheduler that does something specific like making sure that all Tasks for that scheduler always operate on a single thread.

So think of Tasks as items in your to-do list. You might be able to do 5 things at once, but if your boss gives you 10000, they will pile up in your inbox until the first 5 that you are doing get done. The difference between Tasks and the ThreadPool is that Tasks (as I mentioned earlier) give you better control over the relationship between different items of work (imagine to-do items with multiple instructions stapled together), whereas the ThreadPool just allows you to queue up a bunch of individual, single-stage items (Functions).



回答2:

You are hearing two different notions of task. The first is the notion of a job, and the second is the notion of a process.

A long time ago (in computer terms), there were no threads. Each running instance of a program was called a process, since it simply performed one step after another after another until it exited. This matches the intuitive idea of a process as a series of steps, like that of a factory assembly line. The operating system manages the process abstraction.

Then, developers began to add multiple assembly lines to the factories. Now a program could do more than one thing at once, and either a library or (more commonly today) the operating system would manage the scheduling of the steps within each thread. A thread is kind of a lightweight process, but a thread belongs to a process, and all the threads in a process share memory. On the other hand, multiple processes can't mess with each others' memory. So, the multiple threads in your web server can each access the same information about the connection, but Word can't access Excel's in-memory data structures because Word and Excel are running as separate processes. The idea of a process as a series of steps doesn't really match the model of a process with threads, so some people took to calling the "abstraction formerly known as a process" a task. This is the second definition of task that you saw in the blog post. Note that plenty of people still use the word process to mean this thing.

Well, as threads became more commmon, developers added even more abstractions over top of them to make them easier to use. This led to the rise of the thread pool, which is a library-managed "pool" of threads. You pass the library a job, and the library picks a thread and runs the job on that thread. The .NET framework has a thread pool implementation, and the first time you heard about a "task" the documentation really meant a job that you pass to the thread pool.

So in a sense, both the documentation and the blog post are right. The overloading of the term task is the unfortunate source of confusion.



回答3:

Threads have been a part of .Net from v1.0, Tasks were introduced in the Task Parallel Library TPL which was released in .Net 4.0.

You can consider a Task as a more sophisticated version of a Thread. They are very easy to use and have a lot of advantages over Threads as follows:

  1. You can create return types to Tasks as if they are functions.
  2. You can the "ContinueWith" method, which will wait for the previous task and then start the execution. (Abstracting wait)
  3. Abstracts Locks which should be avoided as per guidlines of my company.
  4. You can use Task.WaitAll and pass an array of tasks so you can wait till all tasks are complete.
  5. You can attach task to the parent task, thus you can decide whether the parent or the child will exist first.
  6. You can achieve data parallelism with LINQ queries.
  7. You can create parallel for and foreach loops
  8. Very easy to handle exceptions with tasks.
  9. *Most important thing is if the same code is run on single core machine it will just act as a single process without any overhead of threads.

Disadvantage of tasks over threads:

  1. You need .Net 4.0
  2. Newcomers who have learned operating systems can understand threads better.
  3. New to the framework so not much assistance available.

Some tips:- Always use Task.Factory.StartNew method which is semantically perfect and standard.

Take a look at Task Parallel Libray for more information http://msdn.microsoft.com/en-us/library/dd460717.aspx



回答4:

Expanding on the comment by Eric Lippert:

Threads are a way that allows your application to do several things in parallel. For example, your application might have one thread that processes the events from the user, like button clicks, and another thread that performs some long computation. This way, you can do two different things “at the same time”. If you didn't do that, the user wouldn't be to click buttons until the computation finished. So, Thread is something that can execute some code you wrote.

Task, on the other hand represents an abstract notion of some job. That job can have a result, and you can wait until the job finishes (by calling Wait()) or say that you want to do something after the job finishes (by calling ContinueWith()).

The most common job that you want to represent is to perform some computation in parallel with the current code. And Task offers you a simple way to do that. How and when the code actually runs is defined by TaskScheduler. The default one uses a ThreadPool: a set of threads that can run any code. This is done because creating and switching threads in inefficient.

But Task doesn't have to be directly associated with some code. You can use TaskCompletionSource to create a Task and then set its result whenever you want. For example, you could create a Task and mark it as completed when the user clicks a button. Some other code could wait on that Task and while it's waiting, there is no code executing for that Task.

If you want to know when to use Task and when to use Thread: Task is simpler to use and more efficient that creating your own Threads. But sometimes, you need more control than what is offered by Task. In those cases, it makese sense to use Thread directly.



回答5:

Tasks really are just a wrapper for the boilerplate code of spinning up threads manually. At the root, there is no difference. Tasks just make the management of threads easier, as well as they are generally more expressive due to the lessening of the boilerplate noise.