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

推荐订阅源

S
Security @ Cisco Blogs
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
C
Check Point Blog
爱范儿
爱范儿
A
About on SuperTechFans
博客园 - 聂微东
V
Visual Studio Blog
博客园_首页
WordPress大学
WordPress大学
Help Net Security
Help Net Security
博客园 - Franky
Forbes - Security
Forbes - Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Webroot Blog
Webroot Blog
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
量子位
Vercel News
Vercel News
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
V
V2EX
SecWiki News
SecWiki News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Securelist
L
LangChain Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Schneier on Security
Schneier on Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Register - Security
The Register - Security
L
Lohrmann on Cybersecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cyber Attacks, Cyber Crime and Cyber Security
Simon Willison's Weblog
Simon Willison's Weblog
Apple Machine Learning Research
Apple Machine Learning Research
Security Archives - TechRepublic
Security Archives - TechRepublic
Latest news
Latest news
Spread Privacy
Spread Privacy
F
Full Disclosure
美团技术团队
I
Intezer

博客园 - JiaLiWei

Web、WCF和WS通过Nginx共享80端口 TFS2017持续发布中调用PowerShell启停远程应用程序 基于BUI开发Asp.net MVC项目 WebAPI应用问题整理 TFS下载文件已损坏问题 Asp.net core中使用Session 为什么使用.Net Core, Asp.net Core以及部署到云端 基于微软开发平台构建和使用私有NuGet托管库 TFS2017代码搜索功能 Python sphinx-build在Windows系统中生成Html文档 Oracle PL/SQL Developer集成TFS进行团队脚本文件版本管理 Gulp自动构建Web前端程序 TFS应用经验-大型项目数据仓库抽取导致的TFS应用无法访问 TFS实现需求工作项自动级联保存 TFS 测试用例导入、导出工具 开发团队在TFS中使用Git Repository (二) 开发团队在TFS中使用Git Repository (一) TFS Services 集成Docker TFS 测试用例步骤数据统计
C#多线程顺序依赖执行控制
JiaLiWei · 2017-05-05 · via 博客园 - JiaLiWei

在开发过程中,经常需要多个任务并行的执行的场景,同时任务之间又需要先后依赖的关系。针对这样的处理逻辑,通常会采用多线程的程序模型来实现。

比如A、B、C三个线程,A和B需要同时启动,并行处理,且B需要依赖A完成,在进行后续的处理,C需要B完成后开始处理。

针对这个场景,使用了ThreadPool,ManualResetEvent等.net框架内置的类功能进行了模拟,实现代码如下:

public class MultipleThreadCooperationSample

    {

        public static ManualResetEvent eventAB = new ManualResetEvent(false);

 

        public static ManualResetEvent eventBC = new ManualResetEvent(false);

 

        public static int Main(string[] args)

        {

            //so called thread A

            ThreadPool.QueueUserWorkItem(new WaitCallback(d =>

            {

                Console.WriteLine("Start A thread");

                Thread.Sleep(4000);

                eventAB.Set();

            }));

 

            //thread A

            ThreadPool.QueueUserWorkItem(new WaitCallback(d =>

            {

                Console.WriteLine("Start B thread and wait A thread to finised.");

                eventAB.WaitOne();

               

                Console.WriteLine("Process something within B thread");

 

                Thread.Sleep(4000);

                eventBC.Set();

            }));

 

 

            eventBC.WaitOne(Timeout.Infinite, true);

            //thread C

            ThreadPool.QueueUserWorkItem(new WaitCallback(d =>

            {

                Console.WriteLine("From C thread, everything is done.");

            }));

 

            Console.ReadLine();

 

            return 0;

        }

}

 

运行结果如下: