收藏来自http://blog.csdn.net/zhoufoxcn/article/details/2453803 的文章
2012-12-07 22:29
344 查看
http://blog.csdn.net/zhoufoxcn/article/details/2453803
在开发中经常会遇到线程的例子,如果某个后台操作比较费时间,我们就可以启动一个线程去执行那个费时的操作,同时程序继续执行。在某些情况下可能会出现多个线程的同步协同的问题,下面的例子就展示了在两个线程之间如何协同工作。
这个程序的思路是共同做一件事情(从一个ArrayList中删除元素),如果执行完成了,两个线程都停止执行。
代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
/// <summary>
/// 在开发中经常会遇到线程的例子,如果某个后台操作比较费时间,我们就可以启动一个线程去执行那个费时的操作,同时程序继续执行。在某些情况下可能会出现多个线程的同步协同的问题,下面的例子就展示了在两个线程之间如何协同工作。
///
///这个程序的思路是共同做一件事情(从一个ArrayList中删除元素),如果执行完成了,两个线程都停止执行。
///作者:周公
/// 时间:2008-5-17
/// 原发地址:http://blog.csdn.net/zhoufoxcn
/// </summary>
public
class ThreadDemo
{
private Thread threadOne;
private Thread threadTwo;
private ArrayList stringList;
private
event EventHandler OnNumberClear;//数据删除完成引发的事件
public
static
void Main()
{
ThreadDemo demo =
new ThreadDemo(1000);
demo.Action();
}
public ThreadDemo(int number)
{
Random random =
new Random(1000000);
stringList =
new ArrayList(number);
for (int i
= 0; i
< number; i++)
{
stringList.Add(random.Next().ToString());
}
threadOne =
new Thread(new ThreadStart(Run));//两个线程共同做一件事情
threadTwo =
new Thread(new ThreadStart(Run));//两个线程共同做一件事情
threadOne.Name =
"线程1";
threadTwo.Name =
"线程2";
OnNumberClear +=
new EventHandler(ThreadDemo_OnNumberClear);
}
///
<summary>
/// 开始工作
///
</summary>
public
void Action()
{
threadOne.Start();
threadTwo.Start();
}
///
<summary>
/// 共同做的工作
///
</summary>
private
void Run()
{
string stringValue
= null;
while (true)
{
Monitor.Enter(this);//锁定,保持同步
stringValue = (string)stringList[0];
Console.WriteLine(Thread.CurrentThread.Name +
"删除了"
+ stringValue);
stringList.RemoveAt(0);//删除ArrayList中的元素
if (stringList.Count
== 0)
{
OnNumberClear(this,
new EventArgs());//引发完成事件
}
Monitor.Exit(this);//取消锁定
Thread.Sleep(5);
}
}
//执行完成之后,停止所有线程
void ThreadDemo_OnNumberClear(object sender, EventArgs e)
{
Console.WriteLine("执行完了,停止了所有线程的执行。");
threadTwo.Abort();
threadOne.Abort();
}
}
说明:要实现线程同步不止这一种方式。在这里采用了事件,在事件处理程序里中止了线程(主要是回答csdn论坛的一个朋友的问题才用了这种办法)。
原文出处:
http://blog.csdn.net/zhoufoxcn/article/details/2453803
在开发中经常会遇到线程的例子,如果某个后台操作比较费时间,我们就可以启动一个线程去执行那个费时的操作,同时程序继续执行。在某些情况下可能会出现多个线程的同步协同的问题,下面的例子就展示了在两个线程之间如何协同工作。
这个程序的思路是共同做一件事情(从一个ArrayList中删除元素),如果执行完成了,两个线程都停止执行。
代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
/// <summary>
/// 在开发中经常会遇到线程的例子,如果某个后台操作比较费时间,我们就可以启动一个线程去执行那个费时的操作,同时程序继续执行。在某些情况下可能会出现多个线程的同步协同的问题,下面的例子就展示了在两个线程之间如何协同工作。
///
///这个程序的思路是共同做一件事情(从一个ArrayList中删除元素),如果执行完成了,两个线程都停止执行。
///作者:周公
/// 时间:2008-5-17
/// 原发地址:http://blog.csdn.net/zhoufoxcn
/// </summary>
public
class ThreadDemo
{
private Thread threadOne;
private Thread threadTwo;
private ArrayList stringList;
private
event EventHandler OnNumberClear;//数据删除完成引发的事件
public
static
void Main()
{
ThreadDemo demo =
new ThreadDemo(1000);
demo.Action();
}
public ThreadDemo(int number)
{
Random random =
new Random(1000000);
stringList =
new ArrayList(number);
for (int i
= 0; i
< number; i++)
{
stringList.Add(random.Next().ToString());
}
threadOne =
new Thread(new ThreadStart(Run));//两个线程共同做一件事情
threadTwo =
new Thread(new ThreadStart(Run));//两个线程共同做一件事情
threadOne.Name =
"线程1";
threadTwo.Name =
"线程2";
OnNumberClear +=
new EventHandler(ThreadDemo_OnNumberClear);
}
///
<summary>
/// 开始工作
///
</summary>
public
void Action()
{
threadOne.Start();
threadTwo.Start();
}
///
<summary>
/// 共同做的工作
///
</summary>
private
void Run()
{
string stringValue
= null;
while (true)
{
Monitor.Enter(this);//锁定,保持同步
stringValue = (string)stringList[0];
Console.WriteLine(Thread.CurrentThread.Name +
"删除了"
+ stringValue);
stringList.RemoveAt(0);//删除ArrayList中的元素
if (stringList.Count
== 0)
{
OnNumberClear(this,
new EventArgs());//引发完成事件
}
Monitor.Exit(this);//取消锁定
Thread.Sleep(5);
}
}
//执行完成之后,停止所有线程
void ThreadDemo_OnNumberClear(object sender, EventArgs e)
{
Console.WriteLine("执行完了,停止了所有线程的执行。");
threadTwo.Abort();
threadOne.Abort();
}
}
说明:要实现线程同步不止这一种方式。在这里采用了事件,在事件处理程序里中止了线程(主要是回答csdn论坛的一个朋友的问题才用了这种办法)。
原文出处:
http://blog.csdn.net/zhoufoxcn/article/details/2453803
相关文章推荐
- 收藏:Java开发者的十大戒律(来自http://news.csdn.net/n/20071126/111088.html) 文章已不在,但是经典
- 选择Java接口还是抽象类 本篇文章来自IDC专家网 原文链接:http://www.soidc.net/articles/1213781061058/20001129/1214154133658_1.html
- DataControlRowState 枚举文章来自 中国3A站长学院(www.youaaa.com) - 详文链接:http://www.youaaa.com/html/aspnet/advanced/200812/08-6201_4.h
- 哈哈,收藏源代码(本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hakule/archive/2008/09/01/2861688.aspx)
- linux下的时间 文章来自:http://wenwen.sogou.com/z/q311998900.htm
- Xamarin体验:使用C#开发iOS/Android应用(此文章为收藏博客,不是个人经验) by----作者:囧月 出处:http://lwme.cnblogs.com/
- vector中的find 文章来自网络,如有侵权,请及时联系,马上删除http://blog.csdn.net/huangyimin/article/details/6133650
- 复制文章时自动在文章末尾加上来源网址的代码本文来自: http://www.cnblogs.com/wifi 详细来源请参考:http://www.cnblogs.com/wifi/archive/2012/04/24/2468097.html
- Struts中不同的Action和ActionForm组合(在技术中心看到的文章,觉得有用,所以自己收藏一下)引自http://blog.csdn.net/sean_gao/
- http收藏的文章
- http://subject.csdn.net/SQL-I/SQL注入漏洞全接触2009-08-01 来自:杜倩 [收藏到我的网摘]
- 数据分页 文章来自http://blog.joycode.com/microhelper/articles/1265.aspx
- 《webpack系列文章收藏》
- 来自HeroKu的HTTP API 设计指南(中文版)
- 部分个人收藏文章(python)
- DP基础问题若干(ACM/ICPC) 来自http://blog.csdn.net/accry/article/details/6607593
- 来自另一篇Java Annotation文章
- 挺有意思的一篇转过来的文章 收藏一下:P
- js实现收藏文章
- 来自日本程序员的MIDP 1.0 HttpConnection类的robust封装