任务(System.Threading.Task)和线程之间的区别 [英] Difference between Task (System.Threading.Task) and Thread

查看:432
本文介绍了任务(System.Threading.Task)和线程之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解有关任务和放大器之间的差异;线程是任务发生在线程池,而线程是,我需要通过自己管理的东西..(和任务可以被取消,并返回到线程池在他的任务结束)

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 ... )

推荐答案

我的认为的是什么,你在谈论当你说任务是<一个href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.aspx">System.Threading.Task.如果是这样的话,那么你可以考虑一下这种方式:

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:

  • 在一个程序可以有多个线程,但一个处理器内核只能一次运行一个线程。
    • 线程是非常昂贵,并且正在运行的线程之间的切换也是非常昂贵的。
    • 所以......有成千上万的做的东西是低效的线程。试想一下,如果你的老师给的 10,000任务做。你会花他们之间那么多的时间骑自行车,你从来没有做成任何事情。同样的事情会发生到CPU,如果你开始线程太多。
    • 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.

      要解决这个问题,在.NET框架允许您创建任务。任务是有点捆绑成一个对象的工作,他们让你做有趣的事情像采集的工作和链条件作品的输出在一起(第一的去商店,的那么的买杂志)。

      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).

      任务被安排在一个线程池。线程的具体数目取决于所使用的调度程序,但默认调度程序试图挑一些线程的最适合你有和你的任务是多少时间实际花费使用CPU时间的CPU内核的数量。如果你愿意,你甚至可以编写自己的调度程序,做具体的像确保所有的任务是调度在单个线程总是在工作什么的。

      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.

      因此​​,认为任务是在你的待办事项列表项。你也许可以做的5件事情,但如果你的老板给你10000,他们将堆积在收件箱中,直到第5,你正在做得到完成。任务和线程池之间的不同之处在于任务(如我前面所提到的),给你更好地控制工作的不同项目之间的关系(想象,做装订在一起多条指令项目),而线程池只是让你排队一群个人,单级项目(功能)。

      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).

      这篇关于任务(System.Threading.Task)和线程之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆