C#自訂線程

來源:互聯網
上載者:User
二部分組成,一個線程管理類ThreadManager,一個線程類MyThread
Test類是用來測試的
1.ThreadManager.cs

using System;
using System.Threading;
using System.Collections;
namespace CustomThreadPool
{
/// <summary>
/// 線程管理器,會開啟或喚醒一個線程去執行指定的回調方法
/// </summary>
public class ThreadManager
{
private static ArrayList threadList = new ArrayList(); //線程列表,靜態
//不允許建立執行個體
private ThreadManager()
{
}
/// <summary>
/// 靜態方法,開啟或喚醒一個線程去執行指定的回調方法
/// </summary>
/// <param >委託執行個體</param>
/// <param >傳遞給回調方法的參數</param>
/// <param >當沒有可用的線程時的等待時間,以毫秒為單位</param>
/// <returns></returns>
public static bool QueueUserWorkItem(WaitCallback waitCallback, Object obj, int timeOut)
{
//鎖住共用資源,實現安全執行緒
lock(threadList)
{
try
{
//如果線程列表為空白,填充線程列表
if (threadList.Count == 0)
{
InitThreadList();
}
long startTime = DateTime.Now.Ticks;
do
{
//遍曆線程列表,找出可用的線程
foreach(MyThread myThread in threadList)
{
//線程為空白,需要建立線程
if (myThread.T == null)
{
myThread.Start(waitCallback, obj, false);
return true;
}
else

 if (myThread.T.ThreadState == ThreadState.Suspended)
 {

  //線程為掛起狀態,喚醒線程
  myThread.Start(waitCallback, obj, true);
  return true;
 }
}
//線上程 Sleep 前釋放鎖
Monitor.PulseAll(threadList);
Thread.Sleep(500);
}

while (((DateTime.Now.Ticks - startTime) / 10000) < timeOut);

}

finally

{

Monitor.Exit(threadList);

}

}

return false;

}

//使用 MyThread 對象填充線程列表,注意,這個時候線程並沒有啟動

private static void InitThreadList()

{

threadList = new ArrayList();

for (int i = 0; i < 10; i++)

{

MyThread t = new MyThread();

threadList.Add(t);

}

}

}

}

2.MyThread.cs

using System;

using System.Threading;

namespace CustomThreadPool

{

/// <summary>

/// 封裝 .NET 架構提供的 Thread

/// </summary>

internal class MyThread

{

private Thread t; //線程

private WaitCallback w; //委託,這裡直接用 .NET 架構內建的,也可以根據需要自己定義一個

private Object o; //傳遞給符合委託的回調方法的參數值,根據委託的定義而定

/// <summary>

/// 執行回調方法的線程

/// </summary>

public Thread T

{

get

{

return t;

}

}

public MyThread()

{

}

/// <summary>

/// 開啟新線程或喚醒線程,去執行回調方法

/// </summary>

/// <param >用回調方法執行個體化了的委託執行個體</param>

/// <param >傳遞給回調方法的參數值</param>

/// <param >true 表示線程為掛起狀態,false 則表示線程還沒建立</param>

public void Start(WaitCallback w, Object o, bool isSuspend)

{

//開啟新線程或喚醒線程前,先設定

this.w = w;

this.o = o;

//線程為掛起狀態,喚醒線程繼續執行

if (isSuspend)

{

t.Resume();

}

else

{//線程還沒有建立,建立一個新線程,並執行

t = new Thread(new ThreadStart(this.ThreadProc));

t.Start();

}

}

/// <summary>

/// 線程執行的方法

/// </summary>

private void ThreadProc()

{

//死迴圈,使線程喚醒後不是退出,而是繼續通過委託執行回調方法

while (true)

{

//通過委託執行回調方法

w(o);

t.Suspend();

}

}

}

}

3.Test.cs

using System;

using System.Threading;

namespace CustomThreadPool

{

/// <summary>

/// 測試自訂線程池

/// </summary>

class Test

{

/// <summary>

/// 應用程式的主進入點。

/// </summary>

[STAThread]

static void Main(string[] args)

{

//

// TODO: 在此處添加代碼以啟動應用程式

//

for (int i = 0; i < 5; i++)

{

Console.WriteLine("Start thread {0}", i.ToString());

Thread t = new Thread(new ThreadStart(WorkThread));

t.Start();

}

Console.ReadLine();

Thread.CurrentThread.Abort();

}

public static void WorkThread()

{

for (int i = 0; i < 10; i++)

{

if (i % 2 == 0)

{

if (!ThreadManager.QueueUserWorkItem(new WaitCallback(ThreadProcOne), i, 2000))

{

Console.WriteLine("Failed" + i.ToString());

}

}

else

{

if (!ThreadManager.QueueUserWorkItem(new WaitCallback(ThreadProcTwo), i, 2000))

{

Console.WriteLine("Failed" + i.ToString());

}

}

}

Thread.CurrentThread.Abort();

}

public static void ThreadProcOne(Object stateInfo)

{

Console.WriteLine("Test custom threadpool:" + ((int)stateInfo).ToString());

}

public static void ThreadProcTwo(Object stateInfo)

{

Console.WriteLine("Change work:" + ((int)stateInfo).ToString());

}

}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.