手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#通过Semaphore类控制线程队列的方法
C#通过Semaphore类控制线程队列的方法
摘要:本文实例讲述了C#通过Semaphore类控制线程队列的方法。分享给大家供大家参考。具体实现方法如下:usingSystem;usingSy...

本文实例讲述了C#通过Semaphore类控制线程队列的方法。分享给大家供大家参考。具体实现方法如下:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Diagnostics; using System.Threading; using System.ComponentModel; using System.Collections; using System.Net; using System.Runtime.Serialization; using System.Xml; using System.Globalization; using System.Text.RegularExpressions; using System.Data; using System.Data.SqlClient; namespace ConsoleApp { /// <summary> /// 线程控制队列 /// Semaphore类 /// </summary> class Program { static Semaphore semaphore; static void Main(string[] args) { semaphore = new Semaphore(0, 2); Thread thread; for (int i = 0; i <= 5; i++) { thread = new Thread(new ParameterizedThreadStart(Run)); thread.Start("thread_"+i.ToString()); } semaphore.Release(2); Console.ReadLine(); } static void Run(object obj) { semaphore.WaitOne(); Console.WriteLine("thread " + obj.ToString() + " into the method"); System.Threading.Thread.Sleep(5000); Console.WriteLine("_thread " + obj.ToString() + " leave the method"); semaphore.Release(); } } }

希望本文所述对大家的C#程序设计有所帮助。

【C#通过Semaphore类控制线程队列的方法】相关文章:

C#生成sitemap站点地图的方法

C#随机生成Unicode类型字符串

c#解压文件的实例方法

C#连接db2数据库的实现方法

解析StreamReader与文件乱码问题的解决方法

C#中判断本地系统的网络连接状态的方法

.net后台获取html控件值的2种方法

C#访问应用程序配置文件的方法

C# 如何在MVC3中取消备用控制器的选择

c#进度条 progressBar 使用方法的小例子

精品推荐
分类导航