一个多线程的练习
来源:互联网 发布:dbc传奇数据库 编辑:程序博客网 时间:2024/04/28 14:08
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();
}
}
}
- 一个多线程的练习
- 一个关于多线程同步的小练习。
- 自己学习android网络应用部分,练习的一个多线程的服务器和客户端
- 一个简单的练习
- 一个封装的练习
- 一个列表的练习
- 一个javascript的练习
- 练习:有参方法的一个练习
- 多线程练习
- 多线程练习
- 多线程 练习
- 多线程练习
- 多线程练习
- 多线程练习
- 多线程练习
- 黑马程序员-java的多线程的练习
- 一个多线程的问题
- 多线程的一个例子
- 一辈子假积极就是真积极
- 最近在做操作系统
- asp.net开发wap程序必备:识别来访手机品牌型号
- 大五毕业,纪念!
- 解放了[By tina]
- 一个多线程的练习
- sed用法
- 如何从 Java 存储过程将 JDBC ResultSet 作为 Ref Cursor 返回
- 一、C 语 言 的 格 式
- Eclipse CDT 难以忍受的慢
- 使用JDBC连接MySql
- 二、 基 本 资 料 型 态
- 三、储 存 类 别
- 我的电脑终于不自动重启了(至少到现在为止)