当前位置:网站首页>C#多线程学习笔记三
C#多线程学习笔记三
2022-06-10 13:42:00 【HollowKnightZ】
ThreadPool
Thread => 一个异步任务开启一个Thread,具有专有性
ThreadPool => 开启异步任务只需要向其借用线程,使用完后归还
ThreadPool的使用方式
ThreadPool.QueueUserWorkItem((obj) =>
{
var Fun = obj as Func<string>;
Console.WriteLine("子线程:" + Thread.CurrentThread.ManagedThreadId + " context:" + Fun());
}, new Func<string>(() => "hello world"));
Console.WriteLine("主线程:" + Thread.CurrentThread.ManagedThreadId);
Console.ReadKey();
ThreadPool与Thread的区别
10个任务,用Thread来做,需要开启10个Thread,如果用ThreadPool来做,只需要将10个任务交给线程池。
Thread的解析函数如下:
~Thread()
{
this.InternalFinalize();
}
从析构函数看到this.InternalFinalize(); 就是说销毁之后,先进入终结器,线程虽然销毁,但是没有被GC(Garbage Collection)回收,该占的资源还是会占。而使用ThreadPool后不会有死线程,而都是默认初始化的。
线程池中的线程:工作线程和IO线程

工作线程:给一般的异步任务执行,不涉及到网络,文件这些IO操作【开 发者调用】
IO线程:一般用在文件,网络IO上。【CLR调用】
总结
ThreadPool可以用8个线程来解决 thread 10个线程做的事情,节省了空间时间。
时间 => 通知各个托管和非托管的DLL
空间 => teb,osthread结构,堆栈
边栏推荐
- 聊聊消息中间件(1),AMQP那些事儿
- The shortcomings of the "big model" and the strengths of the "knowledge map"
- Leetcode-57- insert interval
- Gree mobile phone challenge Apple mobile phone? I'm afraid there's nothing else but hard talk
- Google Earth engine (GEE) -- batch download of DEM using MODIS leaf area index image mask
- What needs to be done for mobile app performance testing? How much is the performance test report charged?
- Markdown sets the font to red
- 32. Simple test of raspberry pie serial port communication and ultrasonic module ranging
- [note] about the problem of insufficient compilation mapping memory in keil
- Gin blog 总结1
猜你喜欢
![Buuctf [glassfish] arbitrary file reading](/img/37/e3c127f2f2ba97c5ca0b6cf01cf9ab.png)
Buuctf [glassfish] arbitrary file reading
![buuctf [PHP]XDebug RCE](/img/e2/bcae10e2051b7e9dce918bf87fdc05.png)
buuctf [PHP]XDebug RCE
![buuctf [Discuz]wooyun-2010-080723](/img/b9/d63c9c638ff7c390c490d6fd5eafd1.png)
buuctf [Discuz]wooyun-2010-080723

【技术分析】探讨大世界游戏的制作流程及技术——前期流程篇
![buuctf [PHP]inclusion](/img/02/d328ed84e4641c09c5b1eba3ac6ea9.png)
buuctf [PHP]inclusion

数码管驱动芯片+语音芯片的应用场景介绍,WT588E02B-24SS

Win10 virtual machine download and installation process
![buuctf [PHP]CVE-2019-11043](/img/ba/d97fe48acfd20daa66d47f34d99cf1.png)
buuctf [PHP]CVE-2019-11043

「大模型」之所短,「知识图谱」之所长

机器学习中训练和验证指标曲线图能告诉我们什么?
随机推荐
Flutter学习个人总结1
【抬杠C#】如何实现接口的base调用
格力手机叫板苹果手机?除了嘴硬之外,恐怕再无其他
【C语言】指针函数与函数指针、数组函数
Markdown Title centered
大厂必备的40个方法论
解决跨海高并发崩溃难题?so easy
《软件体系结构原理、方法与实践》第二版期末考试复习总结
buuctf [PHP]XDebug RCE
buuctf [PHP]inclusion
Cardview usage and properties
新功能|Mail GPU Counter模块新增GPU图元处理和GPU Shader Cycles
[technical analysis] discuss the production process and technology of big world games - preliminary process
Gree mobile phone challenge Apple mobile phone? I'm afraid there's nothing else but hard talk
高性能实战Alibaba Sentinel笔记,深度还原阿里微服务高并发方案
Flutter Listview, Column, Row学习个人总结2
buuctf [Jupyter]notebook-rce
Use of 5.8G microwave radar module, working principle and introduction of 5.8G microwave radar module
5.8G微波雷达模块使用,5.8G微波雷达模块工作原理和介绍
[cloud computing] what is the relationship between a multi cloud management platform and a public cloud?