关于.net中的mutex
来源:互联网 发布:mac双系统win驱动 编辑:程序博客网 时间:2024/05/16 11:18
今天在做项目的过程中,由于在执行schedule的时候,由于SessionFactory 是一个公有的变量!如果每个线程同时进行的话,那么前面执行过的线程的SessionFactory会被后面的一个覆盖,因此看了一下.net中的mutex类!
private static Mutex mut = new Mutex();
private const int numIterations = 1;
private const int numThreads = 3;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
for(int i = 0; i < numThreads; i++)
{
Thread myThread = new Thread(new ThreadStart(MyThreadProc));
myThread.Name = String.Format("Thread{0}", i + 1);
myThread.Start();
}
Console.ReadLine();
}
private static void MyThreadProc()
{
for(int i = 0; i < numIterations; i++)
{
UseResource();
}
}
private static void UseResource()
{
// Wait until it is safe to enter.
mut.WaitOne();
Console.WriteLine("{0} has entered the protected area",
Thread.CurrentThread.Name);
// Place code to access non-reentrant resources here.
// Simulate some work.
//Thread.Sleep(500);
Console.WriteLine("{0} is leaving the protected area/r/n",
Thread.CurrentThread.Name);
// Release the Mutex.
mut.ReleaseMutex();
}
}
private static Mutex mut = new Mutex();
private const int numIterations = 1;
private const int numThreads = 3;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
for(int i = 0; i < numThreads; i++)
{
Thread myThread = new Thread(new ThreadStart(MyThreadProc));
myThread.Name = String.Format("Thread{0}", i + 1);
myThread.Start();
}
Console.ReadLine();
}
private static void MyThreadProc()
{
for(int i = 0; i < numIterations; i++)
{
UseResource();
}
}
private static void UseResource()
{
// Wait until it is safe to enter.
mut.WaitOne();
Console.WriteLine("{0} has entered the protected area",
Thread.CurrentThread.Name);
// Place code to access non-reentrant resources here.
// Simulate some work.
//Thread.Sleep(500);
Console.WriteLine("{0} is leaving the protected area/r/n",
Thread.CurrentThread.Name);
// Release the Mutex.
mut.ReleaseMutex();
}
}
- 关于.net中的mutex
- VB.NET中的线程操作(Thread,Mutex)
- VB.NET中的线程操作(Thread,Mutex)
- 关于Oracle中的latch和mutex
- 关于mutex
- 关于Memcache mutex设计模式的.net实现
- 关于Memcache mutex设计模式的.net实现
- 关于mutex 锁
- Halcon中的Mutex
- 关于.net中的?符号
- 关于.net中的runat
- monitor mutex lock in .net
- .net mutex控制单一进程
- monitor mutex lock in .net
- 关于 mutex 的调试实例
- 关于mutex的一些理解
- 关于mutex的一些思考
- 多线程编程中的锁定(Mutex)
- DataGrid中日期格式化方法
- Java学习之路:少走弯路,就是捷径
- 注意Windows注册表里加载的病毒和流氓软件!
- 编写高性能Web应用程序的10个技巧
- 随笔
- 关于.net中的mutex
- Nothing
- sql 技术
- Ubuntu 6.06 LTS硬盘安装与配置
- 如何选择企业内容管理工具?
- 在一个DataGrid中加入一个DropDownList后如何给DropDownList赋值
- 大项目中项目经理的作用
- 现在是9.1日 开学吧 第四次 加油吧
- 我是软件开发菜鸟