手机
当前位置:查字典教程网 >编程开发 >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类控制线程队列的方法】相关文章:

Winform跨线程操作的简单方法

C# 去除首尾字符或字符串的方法

C#实现对AES加密和解密的方法

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

C#调用Java类的实现方法

C#访问PostGreSQL数据库的方法

c#在控制台输出彩色文字的方法

深入C# winform清除由GDI绘制出来的所有线条或图形的解决方法

C#.NET字符串比较中忽略符号的方法

深入线程安全容器的实现方法

精品推荐
分类导航