C#通過(guò)Semaphore類控制線程隊(duì)列的方法
本文實(shí)例講述了C#通過(guò)Semaphore類控制線程隊(duì)列的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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> /// 線程控制隊(duì)列 /// 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(); } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
WCF如何使用動(dòng)態(tài)代理精簡(jiǎn)代碼架構(gòu)
這篇文章主要介紹了WCF如何使用動(dòng)態(tài)代理精簡(jiǎn)代碼架構(gòu),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03C#使用FileStream循環(huán)讀取大文件數(shù)據(jù)的方法示例
這篇文章主要介紹了C#使用FileStream循環(huán)讀取大文件數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了FileStream文件流的形式循環(huán)讀取大文件的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05詳解C#如何實(shí)現(xiàn)隱式類型轉(zhuǎn)換
Result?類型是許多編程語(yǔ)言中處理錯(cuò)誤的常用方式,包括?C#?的?dotNext?庫(kù)。在本文中,我們將通過(guò)例子回顧?C#?中?using?語(yǔ)句和隱式類型轉(zhuǎn)換的使用,感興趣的可以了解一下2023-01-01C#基于百度AI實(shí)現(xiàn)機(jī)器翻譯功能
眾所周知,基于百度ai開(kāi)發(fā)平臺(tái)我們可以實(shí)現(xiàn)了人臉識(shí)別、文字識(shí)別 、語(yǔ)音識(shí)別等功能。本文將介紹它的另一個(gè)功能,即實(shí)現(xiàn)機(jī)器翻譯,感興趣的可以了解一下2022-01-01C#實(shí)現(xiàn)炫酷啟動(dòng)圖-動(dòng)態(tài)進(jìn)度條效果
這篇文章主要介紹了基于C#實(shí)現(xiàn)炫酷啟動(dòng)圖-動(dòng)態(tài)進(jìn)度條 效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05c#中SqlHelper封裝SqlDataReader的方法
這篇文章主要介紹了c#中SqlHelper封裝SqlDataReader的方法,涉及C#針對(duì)數(shù)據(jù)庫(kù)相關(guān)操作封裝與使用的技巧,需要的朋友可以參考下2015-05-05