雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#通過Semaphore類控制線程隊列的方法

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊:

本文實例講述了C#通過Semaphore類控制線程隊列的方法。分享給大家供大家參考。具體實現(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>
 /// 線程控制隊列
 /// 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#程序設計有所幫助。

上一篇:winform 實現(xiàn)控制輸入法

欄    目:C#教程

下一篇:C#實現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復原窗體的方法

本文標題:C#通過Semaphore類控制線程隊列的方法

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/7024.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有