C#中lock,Monitor和Mutex的测试代码
来源:互联网 发布:游戏编程入门教程 编辑:程序博客网 时间:2024/05/16 10:01
关于lock,Monitor和Mutex他们同一线程不互斥的测试代码如下:
lock
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace TestThread{ class Program { static void Main(string[] args) { Thread t1 = new Thread(ThreadFunc1); Thread t2 = new Thread(ThreadFunc2); t1.Start(); t2.Start(); } private static object obj = new object(); private static Mutex m = new Mutex(); static void ThreadFunc1() { lock (obj) { for (int i = 0; i < 10; i++) { lock (obj) { Func("ThreadFunc1"); } } } } static void ThreadFunc2() { lock (obj) { for (int i = 0; i < 10; i++) { lock (obj) { Func("ThreadFunc2"); } } } } static void Func(string str) { Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString()); Thread.Sleep(500); } }}
Mutex
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace TestThread{ class Program { static void Main(string[] args) { Thread t1 = new Thread(ThreadFunc1); Thread t2 = new Thread(ThreadFunc2); t1.Start(); t2.Start(); } private static object obj = new object(); private static Mutex m = new Mutex(); static void ThreadFunc1() { m.WaitOne(); for (int i = 0; i < 10; i++) { m.WaitOne(); Func("ThreadFunc1"); m.ReleaseMutex(); } m.ReleaseMutex(); } static void ThreadFunc2() { m.WaitOne(); for (int i = 0; i < 10; i++) { m.WaitOne(); Func("ThreadFunc2"); m.ReleaseMutex(); } m.ReleaseMutex(); } static void Func(string str) { Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString()); Thread.Sleep(500); } }}
Monitor
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace TestThread{ class Program { static void Main(string[] args) { Thread t1 = new Thread(ThreadFunc1); Thread t2 = new Thread(ThreadFunc2); t1.Start(); t2.Start(); } private static object obj = new object(); private static Mutex m = new Mutex(); static void ThreadFunc1() { Monitor.Enter(obj); for (int i = 0; i < 10; i++) { Monitor.Enter(obj); Func("ThreadFunc1"); Monitor.Exit(obj); } Monitor.Exit(obj); } static void ThreadFunc2() { Monitor.Enter(obj); for (int i = 0; i < 10; i++) { Monitor.Enter(obj); Func("ThreadFunc2"); Monitor.Exit(obj); } Monitor.Exit(obj); } static void Func(string str) { Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString()); Thread.Sleep(500); } }}
0 0
- C#中lock,Monitor和Mutex的测试代码
- 再看c#中的lock monitor 和mutex
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中使用Monitor类、Lock和Mutex类来同步多线程的执行
- C#中的lock、Monitor、Mutex
- C#中运用Monitor类、Lock和Mutex类来同步多线程地执行
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(中) (转)
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(中)
- 05-0. 求序列前N项和(15)
- S5P4418启动镜像分析
- swift使用md5
- Facade 模式
- UITableView的优化
- C#中lock,Monitor和Mutex的测试代码
- 开始刷leetcode day59: Unique Paths
- API Hook基本原理和实现
- x-code 升级后,解决第三方插件无效
- C#实现ICON图标的转换程序介绍
- 如何通过python调用新浪微博的API
- VS+Xamarin
- Unity FingerGestures 手势插件教程
- SCTP协议的进一步理解