(六)线程--分别用lock以及Interlocked和Monitor类实现线程的临界区操作(互斥)(示例下载)
2007-12-14 18:01
627 查看
(一).描述
此示例演示分别用lock以及Interlocked和Monitor类实现线程的临界区操作(互斥)
(二).代码
using System;
using System.Threading;
using System.Collections;
namespace 加锁_实现临界区互斥操作_
{
//委托声明(函数签名)
delegate string MyMethodDelegate();
class MyClass
{
private static ArrayList arrList = new ArrayList();
private static int i = 0;
public static void Add()
{
//方法一:用 lock 实现
// lock(arrList)
// {
// arrList.Add(i.ToString());
// i++;
// }
//方法二: 用Interlicked类实现
// System.Threading.Interlocked.Increment(ref i);
// arrList.Add(i.ToString());
//方法三: 用Monitor类实现
try
{
//I.不限时间
//stem.Threading.Monitor.Enter(arrList);
//II.在指定时间获得排他锁
if(System.Threading.Monitor.TryEnter(arrList,TimeSpan.FromSeconds(30))) //在30秒内获取对象排他锁. 灵活运用可以实现防止死锁功能
{ //避免互相等待情况。 在一定时间内得不到排他锁,可能是自己
//占用其它排它锁造成的(别的正在等自己正占用的排它锁,而处于等待状态),
//这时可以释放掉自己正占用的排他锁后,再试图去得到想要的对象的排他锁
arrList.Add(i.ToString());
i++;
}
}
catch
{
//发生异常后自定义错误处理代码
}
finally
{
Monitor.Exit(arrList); //不管是正常还是发生错误,都得释放对象
}
}
[STAThread]
static void Main(string[] args)
{
Thread thread1 = new Thread(new ThreadStart(Add));
Thread thread2 = new Thread(new ThreadStart(Add));
Thread thread3 = new Thread(new ThreadStart(Add));
thread1.Start();
thread2.Start();
thread3.Start();
Console.Read();
for(int i=0;iTrackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=540644
此示例演示分别用lock以及Interlocked和Monitor类实现线程的临界区操作(互斥)
(二).代码
using System;
using System.Threading;
using System.Collections;
namespace 加锁_实现临界区互斥操作_
{
//委托声明(函数签名)
delegate string MyMethodDelegate();
class MyClass
{
private static ArrayList arrList = new ArrayList();
private static int i = 0;
public static void Add()
{
//方法一:用 lock 实现
// lock(arrList)
// {
// arrList.Add(i.ToString());
// i++;
// }
//方法二: 用Interlicked类实现
// System.Threading.Interlocked.Increment(ref i);
// arrList.Add(i.ToString());
//方法三: 用Monitor类实现
try
{
//I.不限时间
//stem.Threading.Monitor.Enter(arrList);
//II.在指定时间获得排他锁
if(System.Threading.Monitor.TryEnter(arrList,TimeSpan.FromSeconds(30))) //在30秒内获取对象排他锁. 灵活运用可以实现防止死锁功能
{ //避免互相等待情况。 在一定时间内得不到排他锁,可能是自己
//占用其它排它锁造成的(别的正在等自己正占用的排它锁,而处于等待状态),
//这时可以释放掉自己正占用的排他锁后,再试图去得到想要的对象的排他锁
arrList.Add(i.ToString());
i++;
}
}
catch
{
//发生异常后自定义错误处理代码
}
finally
{
Monitor.Exit(arrList); //不管是正常还是发生错误,都得释放对象
}
}
[STAThread]
static void Main(string[] args)
{
Thread thread1 = new Thread(new ThreadStart(Add));
Thread thread2 = new Thread(new ThreadStart(Add));
Thread thread3 = new Thread(new ThreadStart(Add));
thread1.Start();
thread2.Start();
thread3.Start();
Console.Read();
for(int i=0;iTrackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=540644
相关文章推荐
- (六)线程--分别用lock以及Interlocked和Monitor类实现线程的临界区操作(互斥)(示例下载)
- (六)线程--分别用lock以及Interlocked和Monitor类实现线程的临界区操作(互斥)(示例下载)
- 上接稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor
- 浅谈C# 中的lock 方法与Monitor 类的关系_以及同步与互斥
- 键盘模拟鼠标(实现用键盘操作鼠标光标)(示例代码下载)
- 分别用(Windows/Form/Soap/WebService)实现用户身份验证(含示例代码下载)
- 稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor
- 基于APM模式的异步实现及跨线程操作窗体或控件方法的实现示例
- 分别用(Windows/Form/Soap/WebService)实现用户身份验证(含示例代码下载)
- iOS多线程编程中,子线程如何访问和操作主线程UI中的视图控件(下载操作用子线程实现)
- Silverlight 2.0使用Lock, Interlocked, EventWaitHandle, Monitor来实现线程同步//C#线程同步的几种方法
- 分别用(Windows/Form/Soap/WebService)实现用户身份验证(含示例代码下载)
- 转载:键盘模拟鼠标(实现用键盘操作鼠标光标)(示例代码下载)
- 线程系列08,实现线程锁的各种方式,使用lock,Montor,Mutex,Semaphore以及线程死锁
- Control Study -> 键盘模拟鼠标(实现用键盘操作鼠标光标)(示例代码下载)
- 用信号量和Posix线程操作来实现双线程高速下载
- 浅谈C# 中的lock 方法与Monitor 类的关系_以及同步与互斥
- Java线程重复执行以及操作共享变量的代码示例
- java并发包中的Condition和Lock 取代Synchronized、wait、notify/notifyAll实现线程的同步与互斥
- 分别用(Windows/Form/Soap/WebService)实现用户身份验证(含示例代码下载)