- 0
- 0
- 约3.7千字
- 约 3页
- 2018-05-26 发布于河南
- 举报
如何利用C#.NET达到同步
a ReaderWriterLock object to control the access. To do this, follow these steps:
Start Microsoft Visual Studio.
On the File menu, point to New, and then click Project.
Click Visual C# Projects under Project Types, and then click Console Application under Templates. Note In Visual Studio 2005 or in Visual Studio 2008, click Visual C# under Project Types, and then click Console Application under Templates.
In the Name text box, type MultiThreadApplication, and then click OK.
Replace the existing code with the following code in Class1.cs.Note In Visual Studio 2005 and in Visual Studio 2008, the default file is Program.cs.
class Class1
{
private ReaderWriterLock rwl = new ReaderWriterLock();
private long myNumber;
public long Number // the Number property
{
get
{
//Acquire a read lock on the resource.
rwl.AcquireReaderLock(Timeout.Infinite);
try
{
Console.WriteLine(Thread:{0} starts getting the Number, Thread.CurrentThread.GetHashCode());
Thread.Sleep(50);
Console.WriteLine(Thread:{0} got the Number, Thread.CurrentThread.GetHashCode());
}
finally
{
//Release the lock.
rwl.ReleaseReaderLock();
}
return myNumber;
}
set
{
//Acquire a write lock on the resource.
rwl.AcquireWriterLock(Timeout.Infinite);
try
{
Console.WriteLine(Thread: {0} start writing the Number, Thread.CurrentThread.GetHashCode());
Thread.Sleep(50);
myNumber = value;
Console.WriteLine(Thread: {
您可能关注的文档
最近下载
- 项目经理面试宝典.pptx VIP
- 高速铁路CRTSⅢ型轨道板先张法预应力体系的技术解析与实践探索.docx VIP
- 云南昆明市第一中学2026届高三下学期2月复习诊断语文试题(含答案)(含解析).docx VIP
- 2026年度江苏旅游职业学院单招《数学》真题附参考答案详解【轻巧夺冠】.docx VIP
- YY 0471.4-2004 接触性创面敷料试验方法 第4部分-舒适性.pdf VIP
- 2026四川成都兴城融晟科技有限公司招聘网络运维工程师、 项目经理的2人备考题库及参考答案详解.docx VIP
- 飞腾平台PSPA1.0白皮书.pdf VIP
- 2025年度苏州工业园区国企社会招聘笔试历年参考题库附带答案详解.docx VIP
- 2026年书记员考试题库100道附完整答案【历年真题】.docx VIP
- 2025年《中华人民共和国药品管理法》培训试卷含答案.docx VIP
原创力文档

文档评论(0)