问题描述
今天,我被挖掘与TPL,发现一类新的Task.Now我只是想知道,什么是任务和线程之间diffrence,哪一个更好?
假设你正在运行的一本书快递公司。你有四辆汽车和四位车手。汽车是一个线程,驱动程序是处理器,而一本书交付的任务。你所面临的问题是如何有效地调度车手和赛车,这样的任务得到完成尽可能快的。
如果事情变得怪异的是,当有比驱动程序(处理器)更多汽车(线程)。会发生什么然后通过行程司机的公园之一的汽车(挂起线程),中间是并进入不同的车(上下文切换),驱动器,一个存在了一段时间执行任务,然后最终还是回到了第一辆车。显然,这是不是有效,因为一名司机住在一辆汽车。
基于任务的并行性的想法是要打破工作纳入的小的任务的可以在将来产生结果的,然后有效地分配完全一样多的线程作为有处理器,这样你就不会浪费时间上下文切换。在实践中,通常不制定出很好的,但是这是这个想法。
这个问题不能回答,因为它没有任何意义。哪个更好,一本书提供给客户,或汽车,以实现它吗?一辆汽车是可以用于递送书的装置;这两件事情都没有的东西,你能理智描述为比其他的好或差。这就像问哪个更好,一个孔或钻?
Today I was digging with TPL and found a new class Task.Now I just wanted to know that what is diffrence between task and Thread,and which one is better?
Suppose you are running a book delivery company. You have four cars and four drivers. A car is a thread, a driver is a processor, and a book delivery is a task. The problem you face is how to efficiently schedule the drivers and cars so that the tasks get done as quickly as possible.
Where things get weird is when there are more cars (threads) than drivers (processors). What happens then is halfway through a trip the driver parks one car (suspends the thread) and gets into a different car (switches context), drives that one around for a while performing tasks, and then eventually comes back to the first car. Obviously that is not as efficient as one driver staying in one car.
The idea of task-based parallism is to break up the work into small tasks that can produce results in the future, and then efficiently allocate exactly as many threads as there are processors so that you don't waste time context switching. In practice, it usually does not work out that nicely, but that's the idea.
The question cannot be answered because it doesn't make any sense. Which is better, a book to deliver to a customer, or a car to deliver it in? A car is a device that can be used to deliver a book; those two things are not things you can sensibly describe as "better" or "worse" than the other. It's like asking "which is better, a hole or a drill?"
这篇关于是任务和线程之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!