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

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - lsyyx

SQL Server 2008中SQL增强之三:Merge(在一条语句中使用Insert,Update,Delete) SQL Server 2008中SQL增强之二:Top新用途 SQL Server 2008中SQL增强之一:Values新用途 SQL Server 2008中增强的汇总技巧 需要弥补的那部分SQL oracle递归查询\sqlserver递归 - lsyyx - 博客园 如何写SQL,计算上下记录同一字段相差值 SQL语句整理资料 Sqlserver Update 用法 可判断datatable的列是否有重复 Oracle:ODP.NET Managed 小试牛刀 (转) Asp.Net4.0/VS2010新变化(4):SEO的改进 Asp.Net4.0/VS2010新变化(2):网站自动预热 Asp.Net4.0/VS2010新变化(3):webform中也可以直接url路由 NET4.0中非常好用的一个东西Tuple 关于.net4.0中的Action委托 NET4.0多线程编程---Tasks .NET4.0多线程编程---Cooperative Cancellation C#4.0语言新功能及应用 (1)
net4.0新特性之线程同步
lsyyx · 2014-10-23 · via 博客园 - lsyyx

有时候我们可能需要使用多线程来执行同一任务,这个任务可能包含多步,而每步之间可能并不相干,但是这个任务必须让所有步骤执行完成后才能够进入下一步。这就如同WF中的并行任务。在.net4.0之前我们可能需要几个类来做到同步。但是现在我们只需要1个类就OK。

复制代码

Console.WriteLine("任务启动"); using (CountdownEvent cd = new CountdownEvent(1)) { for (var i = 0; i < 5; i++) { cd.AddCount(); System.Threading.ThreadPool.QueueUserWorkItem( (o) => { //do something Console.WriteLine("线程:" + Thread.CurrentThread.ManagedThreadId + ",工作启动"); Thread.Sleep(5000); cd.Signal(); Console.WriteLine("线程:" + Thread.CurrentThread.ManagedThreadId + ",工作完毕 "); }); } cd.Signal(); cd.Wait(); }

复制代码

通过以上代码,我们看到只需要使用CountdownEvent类的AddCount() 和 Signal()方法 就可能实现线程同步。

此外,还有一个类也能够实现线程同步:Barrier。然而这个类不是通过使用增加减少信号量来实现同步。在程序执行时我们可呢个为这个类定义需要接到几个信号后同步任务完成进入下一个任务。

Barrier _barrier = new Barrier(3);//接到3个任务后同步目标达成,进入下一个任务。

复制代码

Barrier _bar = new Barrier(3); ThreadPool.QueueUserWorkItem( (o) => { Console.WriteLine(Thread.CurrentThread.ManagedThreadId); _bar.SignalAndWait(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "is complete"); Thread.Sleep(2000); }); ThreadPool.QueueUserWorkItem( (o) => { Console.WriteLine(Thread.CurrentThread.ManagedThreadId); _bar.SignalAndWait(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "is complete"); Thread.Sleep(2000); }); ThreadPool.QueueUserWorkItem( (o) => { Console.WriteLine(Thread.CurrentThread.ManagedThreadId); _bar.SignalAndWait(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "is complete"); Thread.Sleep(2000); }); //以上3个任务完成以后,下一个任务才会执行 ThreadPool.QueueUserWorkItem( (o) => { Thread.Sleep(2000); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); _bar.SignalAndWait(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "is complete"); });

复制代码

使用Task类进行任务控制:

1, 连续任务:

复制代码

Task<int> task1 = new Task<int>(() => { Console.WriteLine("Task1"); return 1; });
Task task2 = task1.ContinueWith( (prev) => { int result = prev.Result; Console.WriteLine("Task2," + result); }); //task1执行完以后执行这个任务 task1.Start();

复制代码

 2, 任务等待:

复制代码

Task t1 = Task.Factory.StartNew(() => { Console.WriteLine("task1"); });
Task t2 = Task.Factory.StartNew(() => { Console.WriteLine("task2"); });
Task t3 = Task.Factory.StartNew(() => { Console.WriteLine("task3"); });
Task.WaitAll(t1, t2, t3); Console.WriteLine("continue");

复制代码

3, 容器任务

复制代码

Task container = new Task(() => { Task.Factory.StartNew(() => { Console.WriteLine("first"); }); Task.Factory.StartNew(() => { Console.WriteLine("second"); }); Task.Factory.StartNew(() => { Console.WriteLine("third"); }); }); container.Start(); // 等待整个任务的完成 container.Wait(); Console.WriteLine("continue"); Console.ReadKey();

复制代码

4, 任务监视

复制代码

Task task1 = new Task( () => { Console.WriteLine("task1 is start"); int c = 0; while (c < 10) { Console.WriteLine(c); Thread.Sleep(1000); c++; } }); task1.Start(); while (!task1.IsCompleted) { Console.WriteLine("task1 not complete,wait..."); Thread.Sleep(1000); } Console.WriteLine("task1 is complete,continue!");

复制代码