当前位置:网站首页>C#多线程学习笔记四
C#多线程学习笔记四
2022-06-10 13:42:00 【HollowKnightZ】
Task from .net 4.0
Task简介
为什么要有Task?
Thread:容易造成时间和空间的开销,而且使用不当容易造成线程过多,导致时间片切换
ThreadPool:控制能力比较弱,比如做Thread的延续,阻塞,取消,超时等功能不能实现,ThreadPool的控制权在CLR,而不在自己。
Task => Thread + ThreadPool + 优化 + 功能扩展
Task看起来像一个Thread,但是在ThreadPool的基础上进行封装并做了性能优化。.net 4.0之后,微软极力推荐Task作为异步计算。
Task的启动方式
启动方式一:实例化方式启动Task
Task task = new Task(()=>
{
Console.WriteLine("tid={0}", Thread.CurrentThread.ManagedThreadId);
});
task.Start();
启动方式二:TaskFactory的方式启动Task
var task = Task.Factory.StartNew(()=>
{
Console.WriteLine("tid={0}", Thread.CurrentThread.ManagedThreadId);
});
启动方式三:Task.Run()的方式启动Task
var task = Task.Run(()=>
{
Console.WriteLine("tid={0}", Thread.CurrentThread.ManagedThreadId);
});
启动方式四:同步执行Task
Task task = new Task(()=>
{
Console.WriteLine("tid={0}", Thread.CurrentThread.ManagedThreadId);
});
task.RunSynchronously ();
Task和ThreadPool
Task底层是由不同的TaskSchedule支撑的,TaskSchedule相当于Task的CPU。默认的TaskSchedule是ThreadPoolScheduler,WPF中的TaskSchedule是SynchronizationContextTaskScheduler, TaskSchedule可以自己定义。
Task的阻塞和延续
阻塞
有以下两个task
Task task1 = new Task(()=>
{
Thread.Sleep(1000);
Console.WriteLine("t1");
});
Task task2 = new Task(() =>
{
Thread.Sleep(2000);
Console.WriteLine("t2");
});
task1.Start();
task2.Start();
var tasks = new Task[2] {
task1, task2 };
(1)WaitAll方法 必须其中所有的Task执行完成才算完成
Task.WaitAll(tasks);
Console.WriteLine("main");
输出顺序为:t1 t2 main
(2)WaitAny方法 只要其中一个Task执行完成就算完成
Task.WaitAny(tasks);
Console.WriteLine("main");
输出顺序为:t1 main t2。
(3)Wait方法 等待操作,用法和效果等同于thread.Join()
上面三个方法的返回值都是void。那么,如果不想阻塞主线程实现WaitAll操作,该怎么做?
t1 t2执行完了执行t3,这就是延续的概念。
阻塞
(1)WhenAll方法
Task.WhenAll(tasks).ContinueWith(new Action<Task>(t=>
{
Console.WriteLine("t3");
}));
输出顺序为:t1 t2 t3。且主线程没有被阻塞。
(2)WhenAny方法
Task.WhenAny(tasks).ContinueWith(new Action<Task>(t=>
{
Console.WriteLine("t3");
}));
输出顺序为:t1 t3 t2。且主线程没有被阻塞。
(3)ContinueWhenAll方法:效果同WhenAll
Task.Factory.ContinueWhenAll(tasks, t =>
{
Console.WriteLine("t3");
});
(4)ContinueWhenAny方法:效果同WhenAny
Task.Factory.ContinueWhenAny(tasks, t =>
{
Console.WriteLine("t3");
});
边栏推荐
- 常见的自动化测试框架有哪些?上海软件测试公司安利
- What is the p value of a gene?
- CardView使用及属性
- [Multisim Simulation] differential amplifier circuit 2
- 2022 high frequency software test interview questions of large factories (with answers)
- 二叉树和图2
- 聊聊消息中间件(1),AMQP那些事儿
- buuctf [Jupyter]notebook-rce
- Celery 异步调用方法改动记录
- Qt: accessing controls in other forms
猜你喜欢

【C语言】指针函数与函数指针、数组函数

Meetup review how Devops & mlops solve the machine learning dilemma in enterprises?
![buuctf [PHP]CVE-2019-11043](/img/ba/d97fe48acfd20daa66d47f34d99cf1.png)
buuctf [PHP]CVE-2019-11043

Flutter drawer学习总结6

2022大厂高频软件测试面试真题(附答案)

【笔记】关于keil中的出现的编译映射内存不足的问题
![[technical analysis] discuss the production process and technology of big world games - preliminary process](/img/8d/52cb98a617f690df310d6de5512ebc.png)
[technical analysis] discuss the production process and technology of big world games - preliminary process

Google Earth engine (GEE) - real time global 10 meter land use / land cover (LULC) data set based on S2 images

Apple邮箱配置QQ邮箱,163邮箱,edu邮箱,gmail邮箱,获取gmail日历

How to locate the hot problem of the game
随机推荐
十款好用跨浏览器测试工具分享,好物值得收藏
typescript入门笔记(个人用)
Flutter Listview, Column, Row学习个人总结2
Ten easy-to-use cross browser testing tools to share, good things worth collecting
H. 265 introduction to coding principles
Docker部署一个Redis集群
32. Simple test of raspberry pie serial port communication and ultrasonic module ranging
How to locate the hot problem of the game
New features mail GPU counter module adds GPU primitive processing and GPU shader cycles
MarkDown 标题居中
TabLayout 使用详解(修改文字大小、下划线样式等)
【笔记】74HC573的一些记录
buuctf [PHP]inclusion
[yellow code] SVN version control tutorial
Record common functions in MySQL at work
解决win10虚拟机和主机不能互相粘贴复制的问题
Smart campus security channel and video monitoring solution
解决跨海高并发崩溃难题?so easy
Qualcomm has finally begun to develop its own core architecture after learning from the difficulties of assembling chips to maintain its competitive advantage
【无标题】音频蓝牙语音芯片,WT2605C-32N实时录音上传技术方案介绍