C# 多线程 AutoResetEvent和ManualResetEvent
来源:互联网 发布:投稿系统php源码 编辑:程序博客网 时间:2024/05/18 18:02
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MutiThreadTest
{
class Program
{
static void Main(string[] args)
{
Thread SeekBookThread = new Thread(new ThreadStart(TestMutiThrd.SeekProc));
SeekBookThread.Name = "扫描线程";
SeekBookThread.Start();
Thread payMoneyThread = new Thread(new ThreadStart(TestMutiThrd.PayMoneyProc));
payMoneyThread.Name = "付钱线程";
payMoneyThread.Start();
Thread getBookThread = new Thread(new ThreadStart(TestMutiThrd.GetBookProc));
getBookThread.Name = "取书线程";
getBookThread.Start();
for (int i = 1; i <= TestMutiThrd.numIterations; i++)
{
Console.WriteLine("买书线程:书本{0}", i);
TestMutiThrd.number = i;
//Signal that a value has been written.
TestMutiThrd.SekEvent.Set();//解锁扫描线程
TestMutiThrd.buyResetEvent.WaitOne();//等待买书所有线程执行结束,才能继续
Thread.Sleep(0);
}
Thread.Sleep(1000);
Console.ReadLine();
SeekBookThread.Abort();
payMoneyThread.Abort();
getBookThread.Abort();
}
}
public class TestMutiThrd
{
public const int numIterations = 10;
//买书
public static AutoResetEvent buyResetEvent = new AutoResetEvent(false);
//扫描
public static AutoResetEvent SekEvent = new AutoResetEvent(false);
//付钱
public static AutoResetEvent PayEvent = new AutoResetEvent(false);
//取书
public static AutoResetEvent GetEvent = new AutoResetEvent(false);
//付钱和取书
public static ManualResetEvent PayGetEvent = new ManualResetEvent(false);
public static ReaderWriterLock RWLock = new ReaderWriterLock();
//static ManualResetEvent myResetEvent = new ManualResetEvent(false);
//static ManualResetEvent ChangeEvent = new ManualResetEvent(false);
public static int number; //这是关键资源
public static TestMutiThrd OTestMutiThrd = new TestMutiThrd();
public int NumIterations
{
get
{
return numIterations;
}
}
/// <summary>
/// 扫描过程
/// </summary>
public static void SeekProc()
{
while (true)
{
SekEvent.WaitOne();
//SekEvent.Reset();
Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
Thread.Sleep(0);
//PayEvent.Set();//解锁付款线程
PayGetEvent.Set();//同时解锁付款和取书线程
}
}
/// <summary>
/// 付钱过程
/// </summary>
public static void PayMoneyProc()
{
while (true)
{
PayGetEvent.WaitOne();
//PayEvent.WaitOne();
Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
Thread.Sleep(10);
//GetEvent.Set();//解锁取书线程
}
}
/// <summary>
/// 取书过程
/// </summary>
public static void GetBookProc()
{
while (true)
{
PayGetEvent.WaitOne();
PayGetEvent.Reset();//不加 无限执行下去
//GetEvent.WaitOne();
Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
Console.WriteLine("------------------------------------------");
Thread.Sleep(10);
buyResetEvent.Set();//解锁买书线程
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MutiThreadTest
{
class Program
{
static void Main(string[] args)
{
Thread SeekBookThread = new Thread(new ThreadStart(TestMutiThrd.SeekProc));
SeekBookThread.Name = "扫描线程";
SeekBookThread.Start();
Thread payMoneyThread = new Thread(new ThreadStart(TestMutiThrd.PayMoneyProc));
payMoneyThread.Name = "付钱线程";
payMoneyThread.Start();
Thread getBookThread = new Thread(new ThreadStart(TestMutiThrd.GetBookProc));
getBookThread.Name = "取书线程";
getBookThread.Start();
for (int i = 1; i <= TestMutiThrd.numIterations; i++)
{
Console.WriteLine("买书线程:书本{0}", i);
TestMutiThrd.number = i;
//Signal that a value has been written.
TestMutiThrd.SekEvent.Set();//解锁扫描线程
TestMutiThrd.buyResetEvent.WaitOne();//等待买书所有线程执行结束,才能继续
Thread.Sleep(0);
}
Thread.Sleep(1000);
Console.ReadLine();
SeekBookThread.Abort();
payMoneyThread.Abort();
getBookThread.Abort();
}
}
public class TestMutiThrd
{
public const int numIterations = 10;
//买书
public static AutoResetEvent buyResetEvent = new AutoResetEvent(false);
//扫描
public static AutoResetEvent SekEvent = new AutoResetEvent(false);
//付钱
public static AutoResetEvent PayEvent = new AutoResetEvent(false);
//取书
public static AutoResetEvent GetEvent = new AutoResetEvent(false);
//付钱和取书
public static ManualResetEvent PayGetEvent = new ManualResetEvent(false);
public static ReaderWriterLock RWLock = new ReaderWriterLock();
//static ManualResetEvent myResetEvent = new ManualResetEvent(false);
//static ManualResetEvent ChangeEvent = new ManualResetEvent(false);
public static int number; //这是关键资源
public static TestMutiThrd OTestMutiThrd = new TestMutiThrd();
public int NumIterations
{
get
{
return numIterations;
}
}
/// <summary>
/// 扫描过程
/// </summary>
public static void SeekProc()
{
while (true)
{
SekEvent.WaitOne();
//SekEvent.Reset();
Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
Thread.Sleep(0);
//PayEvent.Set();//解锁付款线程
PayGetEvent.Set();//同时解锁付款和取书线程
}
}
/// <summary>
/// 付钱过程
/// </summary>
public static void PayMoneyProc()
{
while (true)
{
PayGetEvent.WaitOne();
//PayEvent.WaitOne();
Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
Thread.Sleep(10);
//GetEvent.Set();//解锁取书线程
}
}
/// <summary>
/// 取书过程
/// </summary>
public static void GetBookProc()
{
while (true)
{
PayGetEvent.WaitOne();
PayGetEvent.Reset();//不加 无限执行下去
//GetEvent.WaitOne();
Console.WriteLine("{0}:书本{1}", Thread.CurrentThread.Name, number);
Console.WriteLine("------------------------------------------");
Thread.Sleep(10);
buyResetEvent.Set();//解锁买书线程
}
}
}
}
0 0
- C# 多线程 AutoResetEvent和ManualResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之ManualResetEvent和AutoResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- C#多线程之二:ManualResetEvent和AutoResetEvent
- 多线程ManualResetEvent和AutoResetEvent
- 多线程之AutoResetEvent和ManualResetEvent
- C#多线程同步事件及等待句柄AutoResetEvent 和 ManualResetEvent
- c#多线程之mutex,semaphore,autoresetevent,manualResetevent
- 多线程编程之 AutoResetEvent和ManualResetEvent
- ManualResetEvent和AutoResetEvent——多线程
- C#老生常谈之AutoResetEvent和ManualResetEvent
- C#线程控制ManualResetEvent和AutoResetEvent
- C#笔记20:多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent
- C#笔记20:多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent
- AutoResetEvent和ManualResetEvent
- Android studio 开源日志输入库 Logger无法打印
- windows下静态库使用, 动态库的静态加载, 动态库的动态加载 原创实例!
- std C++ slot signal机制
- lintcode 把排序数组转换为高度最小的二叉搜索树
- 判断字符串String是否为空问题
- C# 多线程 AutoResetEvent和ManualResetEvent
- JS实现继承的几种方式
- 微服务架构的基础框架选择:Spring Cloud还是Dubbo?
- 机器学习(周志华版) 第一章习题1.1个人解答
- 第二册第三单元总结
- 下载html 转pdf
- 关于req.params、req.param(name)、req.query、req.body等请求对象
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle
- Ceph-智能分布CRUSH之Object与PG与OSD