程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#多線程學習(四)多線程的自動管理(線程池)(2)

C#多線程學習(四)多線程的自動管理(線程池)(2)

編輯:關於C語言

ThreadPool 的用法示例:

using System;
using System.Collections;
using System.Threading;
namespace ThreadExample
{
//這是用來保存信息的數據結構,將作為參數被傳遞
public class SomeState
{
   public int CookIE;
   public SomeState(int iCookIE)
   {
Cookie = iCookIE;
   }
}
public class Alpha
{
   public Hashtable HashCount;
   public ManualResetEvent eventX;
   public static int iCount = 0;
   public static int iMaxCount = 0;

public Alpha(int MaxCount)
   {
  HashCount = new Hashtable(MaxCount);
  iMaxCount = MaxCount;
   }
  //線程池裡的線程將調用Beta()方法
   public void Beta(Object state)
   {
   //輸出當前線程的hash編碼值和CookIE的值
  Console.WriteLine(" {0} {1} :", Thread.CurrentThread.GetHashCode(),((SomeState)state).CookIE);
Console.WriteLine("HashCount.Count=={0}, Thread.CurrentThread.GetHashCode()=={1}", HashCount.Count, Thread.CurrentThread.GetHashCode());
lock (HashCount)
{
     //如果當前的Hash表中沒有當前線程的Hash值,則添加之
     if (!HashCount.ContainsKey(Thread.CurrentThread.GetHashCode()))
     HashCount.Add (Thread.CurrentThread.GetHashCode(), 0);
     HashCount[Thread.CurrentThread.GetHashCode()] =
((int)HashCount[Thread.CurrentThread.GetHashCode()])+1;
    }
int iX = 2000;
Thread.Sleep(iX);
//Interlocked.Increment()操作是一個原子操作,具體請看下面說明
Interlocked.Increment(ref iCount);
if (iCount == iMaxCount)
{
    Console.WriteLine();
     Console.WriteLine("Setting eventX ");
     eventX.Set();
  }
   }
}
public class SimplePool
{
public static int Main(string[] args)
{
Console.WriteLine("Thread Pool Sample:");
bool W2K = false;
int MaxCount = 10;//允許線程池中運行最多10個線程
//新建ManualResetEvent對象並且初始化為無信號狀態
ManualResetEvent eventX = new ManualResetEvent(false);
Console.WriteLine("Queuing {0} items to Thread Pool", MaxCount);
Alpha oAlpha = new Alpha(MaxCount);
//創建工作項
//注意初始化oAlpha對象的eventX屬性
oAlpha.eventX = eventX;
Console.WriteLine("Queue to Thread Pool 0");
try
{
//將工作項裝入線程池
//這裡要用到Windows 2000以上版本才有的API,所以可能出現NotSupportException異常
ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta), new SomeState(0));
W2K = true;
}
catch (NotSupportedException)
{
Console.WriteLine("These API's may fail when called on a non-Windows 2000 system.");
W2K = false;
}
if (W2K)//如果當前系統支持ThreadPool的方法.
{
for (int iItem=1;iItem < MaxCount;iItem++)
{
//插入隊列元素
Console.WriteLine("Queue to Thread Pool {0}", iItem);
ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta), new SomeState(iItem));
}
Console.WriteLine("Waiting for Thread Pool to drain");
//等待事件的完成,即線程調用ManualResetEvent.Set()方法
eventX.WaitOne(Timeout.Infinite,true);
//WaitOne()方法使調用它的線程等待直到eventX.Set()方法被調用
Console.WriteLine("Thread Pool has been drained (Event fired)");
Console.WriteLine();
Console.WriteLine("Load across threads");
foreach(object o in oAlpha.HashCount.Keys)
Console.WriteLine("{0} {1}", o, oAlpha.HashCount[o]);
}
Console.ReadLine();
return 0;
}
}
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved