博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# task
阅读量:5318 次
发布时间:2019-06-14

本文共 1971 字,大约阅读时间需要 6 分钟。

Task Parallel Library: 1 of n

Task 与Thread, lambda,  action,funct之间有什么关联。

Task相比Thread提供了什么方便之处。

Thread

1. 创建费时间,占用内存与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 免去了创建thread的时间,用户直接将work item 委托给ThreadPool去安排其内部的线程调用。

2. 但classic threading ThreadPool,当一个work item已经在执行当中,用户不能去直接关闭,想得到其的结果也是很直接。

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: , 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的优点,解决了ThreadPool的问题。其本质也可以讲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.

 

常用的使用场景

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

转载于:https://www.cnblogs.com/pengxinglove/p/5452799.html

你可能感兴趣的文章
Recover Binary Search Tree
查看>>
Java 实践:生产者与消费者
查看>>
[转]IOCP--Socket IO模型终结篇
查看>>
(五)归一化
查看>>
使用信号量
查看>>
实验八 接口与实现接口的类
查看>>
PostgreSQL 保留关键字添加方法之一,不带参数的函数
查看>>
赛前热手 (天梯赛暴力题)
查看>>
【转贴】SAP HANA内存数据库详解
查看>>
两种应该掌握的排序方法--------1.shell Sort
查看>>
vuejs动态组件给子组件传递数据
查看>>
杭电2065(递推)红色病毒
查看>>
js 获取视频的第一帧
查看>>
各种正则验证
查看>>
观察者模式(Observer)
查看>>
python中numpy.r_和numpy.c_
查看>>
WPF简单模拟QQ登录背景动画
查看>>
bzoj 2038 小Z的袜子
查看>>
egret3D与2D混合开发,画布尺寸不一致的问题
查看>>
freebsd 实现 tab 命令 补全 命令 提示
查看>>