using
System;
using System.Threading;
using System.Collections;
namespace TestMultithread
... {
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class MainThread
...{
internal static ArrayList Target;
internal static ArrayList Result;
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
ArrayList lista = new ArrayList();
for(int i=0;i<=100;i++)
...{
lista.Add(i);
}
TestManager m1 = new TestManager();
ArrayList result = m1.Test(lista);
foreach(int item in result)
...{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
/**//// <summary>
/// 其中的doTest方法将为多线程执行的实体
/// </summary>
public class TestQueue
...{
private ArrayList target;
private ArrayList result;
private bool isFinished;
private int targetQueuePosition;
private ManualResetEvent eventX;
public TestQueue(ArrayList toBeTest,ManualResetEvent reseter)
...{
this.Target = toBeTest;
this.Result = new ArrayList();
this.isFinished = false;
this.TargetQueuePosition = 0;
this.EventX = reseter;
}
public ArrayList Target
...{
get...{return target;}
set...{target = value;}
}
public ArrayList Result
...{
get...{return result;}
set...{result = value;}
}
public bool IsFinished
...{
get...{return isFinished;}
set...{isFinished = value;}
}
public int TargetQueuePosition
...{
get...{return targetQueuePosition;}
set...{targetQueuePosition = value;}
}
public ManualResetEvent EventX
...{
get...{return eventX;}
set...{eventX = value;}
}
public ArrayList GetResult()
...{
return this.Result;
}
public void DoTest(object State)
...{
while(true)
...{
Console.WriteLine("threadHashCode: {0} do some work",Thread.CurrentThread.GetHashCode());
if(this.isFinished)
...{
return;
}
int tempData;
lock(this)
...{
if(this.TargetQueuePosition >= this.Target.Count)
...{
return;
}
else
...{
tempData = Convert.ToInt32(this.Target[this.TargetQueuePosition]);
this.TargetQueuePosition++;
}
}
Thread.Sleep(100);
lock(this)
...{
this.Result.Add(tempData);
if(this.Result.Count == this.Target.Count)
...{
this.isFinished = true;
this.EventX.Set();
}
}
}
}
}
public class TestManager
...{
public ArrayList Test(ArrayList toBeTest)
...{
ManualResetEvent eventX = new ManualResetEvent(false);
TestQueue queue = new TestQueue(toBeTest,eventX);
int maxCount = 3;
WaitCallback callback = new WaitCallback(queue.DoTest);
for(int i=0; i<maxCount; i++)
...{
ThreadPool.QueueUserWorkItem(callback);
}
eventX.WaitOne(Timeout.Infinite,true);
return queue.GetResult();
}
}
}
using System.Threading;
using System.Collections;
namespace TestMultithread
... {
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class MainThread
...{
internal static ArrayList Target;
internal static ArrayList Result;
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
ArrayList lista = new ArrayList();
for(int i=0;i<=100;i++)
...{
lista.Add(i);
}
TestManager m1 = new TestManager();
ArrayList result = m1.Test(lista);
foreach(int item in result)
...{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
/**//// <summary>
/// 其中的doTest方法将为多线程执行的实体
/// </summary>
public class TestQueue
...{
private ArrayList target;
private ArrayList result;
private bool isFinished;
private int targetQueuePosition;
private ManualResetEvent eventX;
public TestQueue(ArrayList toBeTest,ManualResetEvent reseter)
...{
this.Target = toBeTest;
this.Result = new ArrayList();
this.isFinished = false;
this.TargetQueuePosition = 0;
this.EventX = reseter;
}
public ArrayList Target
...{
get...{return target;}
set...{target = value;}
}
public ArrayList Result
...{
get...{return result;}
set...{result = value;}
}
public bool IsFinished
...{
get...{return isFinished;}
set...{isFinished = value;}
}
public int TargetQueuePosition
...{
get...{return targetQueuePosition;}
set...{targetQueuePosition = value;}
}
public ManualResetEvent EventX
...{
get...{return eventX;}
set...{eventX = value;}
}
public ArrayList GetResult()
...{
return this.Result;
}
public void DoTest(object State)
...{
while(true)
...{
Console.WriteLine("threadHashCode: {0} do some work",Thread.CurrentThread.GetHashCode());
if(this.isFinished)
...{
return;
}
int tempData;
lock(this)
...{
if(this.TargetQueuePosition >= this.Target.Count)
...{
return;
}
else
...{
tempData = Convert.ToInt32(this.Target[this.TargetQueuePosition]);
this.TargetQueuePosition++;
}
}
Thread.Sleep(100);
lock(this)
...{
this.Result.Add(tempData);
if(this.Result.Count == this.Target.Count)
...{
this.isFinished = true;
this.EventX.Set();
}
}
}
}
}
public class TestManager
...{
public ArrayList Test(ArrayList toBeTest)
...{
ManualResetEvent eventX = new ManualResetEvent(false);
TestQueue queue = new TestQueue(toBeTest,eventX);
int maxCount = 3;
WaitCallback callback = new WaitCallback(queue.DoTest);
for(int i=0; i<maxCount; i++)
...{
ThreadPool.QueueUserWorkItem(callback);
}
eventX.WaitOne(Timeout.Infinite,true);
return queue.GetResult();
}
}
}