惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
S
Securelist
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
Martin Fowler
Martin Fowler
量子位
D
Docker
G
Google Developers Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
U
Unit 42
S
Secure Thoughts
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
K
Kaspersky official blog
NISL@THU
NISL@THU
T
Tailwind CSS Blog
I
Intezer
T
Threat Research - Cisco Blogs
P
Privacy & Cybersecurity Law Blog
D
DataBreaches.Net
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
Vercel News
Vercel News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
F
Fortinet All Blogs
W
WeLiveSecurity
C
Cisco Blogs
Security Latest
Security Latest
PCI Perspectives
PCI Perspectives
L
LangChain Blog
P
Palo Alto Networks Blog
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
Help Net Security
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
Project Zero
Project Zero
G
GRAHAM CLULEY
Recent Commits to openclaw:main
Recent Commits to openclaw:main
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
Lohrmann on Cybersecurity
Cloudbric
Cloudbric
D
Darknet – Hacking Tools, Hacker News & Cyber Security

博客园 - Goodspeed

几种常见的函数 Caesar cipher 遗传算法之背包问题 Transport scheme NOT recognized: [stomp] error running git Canvas 旋转的图片 canvas时钟 火箭起飞 让图标转起来 Tomcat启动脚本 Task中的异常处理 Parallel的陷阱 用Task代替TheadPool 正确停止线程 线程同步中使用信号量AutoResetEvent 异步和多线程的区别 C#和.NET Framework的关系 为什么泛型不支持协变性? 可空值类型与值类型这间的转换
使用ThreadPool代替Thread
Goodspeed · 2014-11-02 · via 博客园 - Goodspeed

线程的空间开销

  1. 线程内核对象。包含上下文信息。32位系统占用700字节
  2. 线程环境块。包括线程的异常处理链。32位系统占用4KB
  3. 用户模式栈。保存方法的参数、局部变量和返回值
  4. 内核模式栈。调用操作系统的内核模式函数时,系统会将函数参数从用户模式栈复制到内核模式栈。32位系统占用12KB

线程的时间开销

  1. 创建时,系统相继初始化上述内存空间
  2. CLR加载DLL到DLLMain方法,并传送连接标志
  3. 线程上下文切换
    1. 进入内核模式
    2. 将上下文信息保存到正在执行的线程内核对象上
    3. 系统获取一个Spinlock,并确定下一个要执行的线程。释放Spinlock。如果下一个线程不在同一个进程,则需要进行虚拟地址交换
    4. 从将被执行的线程内核对象上载入上下文信息
    5. 离开内核模式

使用线程池,CLR不会销毁这个线程,而是会保留这个线程一段时间。

using System;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var p = new Program();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            p.Thread();
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks);
            sw.Restart();
            p.Pool();
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks);
            Console.ReadKey();            
        }

        void Thread()
        {
            for (int i = 0; i < 10; i++)
            {
                var worker = new Thread(() =>
                {
                    //Console.WriteLine("Thread Do");
                });
                worker.Start();
            }
        }

        void Pool()
        {
            for (int i = 0; i < 10; i++)
            {
                ThreadPool.QueueUserWorkItem(state =>
                {
                    //Console.WriteLine("Pool Do");
                });
            }
        }
    }
}