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

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

C#教程

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

C#線程隊列用法實例分析

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

本文實例講述了C#線程隊列用法。分享給大家供大家參考。具體如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ThreadPro
{
 class Program
 {
  static Mutex gM1;
  static Mutex gM2;
  const int ITERS = 100;
  static AutoResetEvent Event1 = new AutoResetEvent(false);
  static AutoResetEvent Event2 = new AutoResetEvent(false);
  static AutoResetEvent Event3 = new AutoResetEvent(false);
  static AutoResetEvent Event4 = new AutoResetEvent(false);
  static void Main(string[] args)
  {
   Console.WriteLine("Mutex Sample ");
   //創(chuàng)建一個Mutex對象,并且命名為MyMutex
   gM1 = new Mutex(true, "MyMutex");
   //創(chuàng)建一個未命名的Mutex 對象.
   gM2 = new Mutex(true);
   Console.WriteLine(" - Main Owns gM1 and gM2");
   AutoResetEvent[] evs = new AutoResetEvent[4];
   evs[0] = Event1; //為后面的線程t1,t2,t3,t4定義AutoResetEvent對象
   evs[1] = Event2;
   Program tm = new Program();
   Thread t1 = new Thread(new ThreadStart(tm.t1Start));
   Thread t2 = new Thread(new ThreadStart(tm.t2Start));
   Thread t3 = new Thread(new ThreadStart(tm.t3Start));
   Thread t4 = new Thread(new ThreadStart(tm.t4Start));
   t1.Start();// 使用Mutex.WaitAll()方法等待一個Mutex數(shù)組中的對象全部被釋放
   t2.Start();// 使用Mutex.WaitOne()方法等待gM1的釋放
   t3.Start();// 使用Mutex.WaitAny()方法等待一個Mutex數(shù)組中任意一個對象被釋放
   t4.Start();// 使用Mutex.WaitOne()方法等待gM2的釋放
   Thread.Sleep(2000);
   Console.WriteLine(" - Main releases gM1");
   gM1.ReleaseMutex(); //線程t2,t3結(jié)束條件滿
   Thread.Sleep(1000);
   Console.WriteLine(" - Main releases gM2");
   gM2.ReleaseMutex(); //線程t1,t4結(jié)束條件滿足
   //等待所有四個線程結(jié)束
   WaitHandle.WaitAll(evs);
   Console.WriteLine(" Mutex Sample");
   Console.ReadLine();
  }
  public void t1Start()
  {
   Console.WriteLine("方法一運行, Mutex.WaitAll(Mutex[])");
   Mutex[] gMs = new Mutex[2];
   gMs[0] = gM1;//創(chuàng)建一個Mutex數(shù)組作為Mutex.WaitAll()方法的參數(shù)
   gMs[1] = gM2;
   Mutex.WaitAll(gMs);//等待gM1和gM2都被釋放
   gM1.ReleaseMutex(); //修正上一次出現(xiàn)的錯誤
   gM2.ReleaseMutex(); //修正上一次出現(xiàn)的錯誤
   Thread.Sleep(2000);
   Console.WriteLine("方法一完畢,WaitAll(Mutex[]) satisfied");
   Event1.Set(); //線程結(jié)束,將Event1設置為有信號狀態(tài)
  }
  public void t2Start()
  {
   Console.WriteLine("方法二運行, gM1.WaitOne( )");
   gM1.WaitOne();//等待gM1的釋放
   gM1.ReleaseMutex(); //修正上一次出現(xiàn)的錯誤
   Console.WriteLine("方法二完畢, gM1.WaitOne( ) satisfied");
   Event2.Set();//線程結(jié)束,將Event2設置為有信號狀態(tài)
  }
  public void t3Start()
  {
   Console.WriteLine("t3Start started, Mutex.WaitAny(Mutex[])");
   Mutex[] gMs = new Mutex[2];
   gMs[0] = gM1;//創(chuàng)建一個Mutex數(shù)組作為Mutex.WaitAny()方法的參數(shù)
   gMs[1] = gM2;
   Mutex.WaitAny(gMs);//等待數(shù)組中任意一個Mutex對象被釋放
   gM1.ReleaseMutex(); //修正上一次出現(xiàn)的錯誤
   Console.WriteLine("t3Start finished, Mutex.WaitAny(Mutex[])");
   Event3.Set();//線程結(jié)束,將Event3設置為有信號狀態(tài)
  }
  public void t4Start()
  {
   Console.WriteLine("t4Start started, gM2.WaitOne( )");
   gM2.WaitOne();//等待gM2被釋放
   gM2.ReleaseMutex(); //修正上一次出現(xiàn)的錯誤
   Console.WriteLine("t4Start finished, gM2.WaitOne( )");
   Event4.Set();//線程結(jié)束,將Event4設置為有信號狀態(tài)
  }
 }
}

希望本文所述對大家的C#程序設計有所幫助。

上一篇:winform創(chuàng)建不規(guī)則窗體的方法

欄    目:C#教程

下一篇:C#實現(xiàn)在Form里面內(nèi)嵌dos窗體的方法

本文標題:C#線程隊列用法實例分析

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

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

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

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

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