c# task
Task Parallel Library: 1 of n
http://www.codeproject.com/Articles/152765/Task-Parallel-Library-of-n
Task 與Thread, lambda,? action,funct之間有什么關(guān)聯(lián)。
Task相比Thread提供了什么方便之處。
Thread
1. 創(chuàng)建費(fèi)時(shí)間,占用內(nèi)存與CPU的資源
For example, when a new Thread is started in .NET, there is a whole process that goes with that, such as creating queues, thread local storage, managing the Thread's lifecycle etc. This takes time
ThreadPool
1. ThreadPool 免去了創(chuàng)建thread的時(shí)間,用戶直接將work item 委托給ThreadPool去安排其內(nèi)部的線程調(diào)用。
2. 但classic threading ThreadPool,當(dāng)一個(gè)work item已經(jīng)在執(zhí)行當(dāng)中,用戶不能去直接關(guān)閉,想得到其的結(jié)果也是很直接。
However, even using the classic threading ThreadPool, there were problems in that you could not cancel a work item once it has been queued with the ThreadPool, or get a return result that easily. It just doesn't read that well either. There is an excellent article here on CodeProject that tackles some of these issues: Smart ThreadPool, which is pretty excellent actually. However, the new TPL infrastructure has got all these problems covered, and many many more useful features in my opinion.
Task
1. the new TPL infrastructure has got all these problems covered, and many many more useful features in my opinion
Task 繼承了ThreadPool的優(yōu)點(diǎn),解決了ThreadPool的問題。其本質(zhì)也可以講Smart ThreadPool
2. A TPL Task actually uses the ThreadPool internally.
?
Task, ThreadPool, Thread, Scheduler
It is worth mentioning that Tasks are merely wrappers for passing a delegate of work to be done, also storing state, exceptions, and continuations amongst others. That work may or may not be done by the threadpool, and as already stated, that will depend upon the scheduler used.
?
常用的使用場(chǎng)景
Tasks also seem to be more inline with how people think about things. For instance, imagine this scenario: "I want to call a Web Service and have it return a List<int>". Using a TPL Task, I would create a Task<List<int>> and get it to call some service in its payload delegate (which will use the ThreadPool) that returned me a List<int>.
?
Task與UI Thread
reate/cancel Tasks and handle Exceptions
轉(zhuǎn)載于:https://www.cnblogs.com/pengxinglove/p/5452799.html
總結(jié)
- 上一篇: vi 编辑器跳转到指定行数
- 下一篇: java 删除文件夹及文件夹中的子文件