About the Monitor object in .NET framework
2009-11-18 19:39
471 查看
Object monitor plays a leading role in thread synchronization. Apart from its Enter and Exit methods (equivalent to opening and closing bracket pair after the lock keyword specifying synchronized code block), it offers two additional methods which are required to augment its synchronization functionality.
According to the documentation, Enter and Exit methods enclose a critical region, in which the object monitored is ensured of being accessed mutually exclusively by different threads.
A thread can relinquish the monitored object it owns by calling Monitor.Wait method, and all the other threads waiting on the object contend for it. DotNet thread mechanism requires the thread releasing the object should call Monitor.Pulse or Monitor.PulseAll to notify all waiting threads that the monitored object's status has been updated before calling the Monitor.Wait. Therefore, as for the code below, the Monitor.Pulse method call right before end of the the lock section is indespensible lest the blocked thread not be woken up.
According to the documentation, Enter and Exit methods enclose a critical region, in which the object monitored is ensured of being accessed mutually exclusively by different threads.
A thread can relinquish the monitored object it owns by calling Monitor.Wait method, and all the other threads waiting on the object contend for it. DotNet thread mechanism requires the thread releasing the object should call Monitor.Pulse or Monitor.PulseAll to notify all waiting threads that the monitored object's status has been updated before calling the Monitor.Wait. Therefore, as for the code below, the Monitor.Pulse method call right before end of the the lock section is indespensible lest the blocked thread not be woken up.
using System; namespace ConsoleTest { class ThreadingTest { void DoCriticalJobs() { lock(this) { for (int i = 0; i < 10; i++) { Console.WriteLine("CriticalJob {0} on thread {1}", i, System.Threading.Thread.CurrentThread.ManagedThreadId); if (i == 4) { System.Threading.Monitor.Pulse(this); System.Threading.Monitor.Wait(this); } } System.Threading.Monitor.Pulse(this); /* this line is essential */ } } void ThreadEntry(object obj) { DoCriticalJobs(); } public void Test() { System.Threading.Thread thread1 = new System.Threading.Thread( new System.Threading.ParameterizedThreadStart(this.ThreadEntry)); System.Threading.Thread thread2 = new System.Threading.Thread( new System.Threading.ParameterizedThreadStart(this.ThreadEntry)); thread1.Priority = System.Threading.ThreadPriority.Normal; thread2.Priority = System.Threading.ThreadPriority.Normal; thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); } } }
相关文章推荐
- About the Monitor object in .NET framework
- org.springframework.security.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityC
- Reference to Null object in the .NET Remoting Deployment?
- The longest class name in Java and .NET Framework
- An object in the SharePoint administrative framework could not be deleted because other objects depend on it.
- What's New in the .NET Framework 2.0 Beta
- New C# Features in the .NET Framework 4
- List of bugs that are fixed in the .NET Framework 1.1 Service Pack 1 (SP1)
- ASP.Net MVC Entity Framework Error – More than one context type was found in the assembly ‘ProjectNa
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- Comparing the Timer Classes in the .NET Framework Class Library
- 转:What's New in the .NET Framework 4
- [转载]How to hide the OK button in the dialog in .Net Compact Framework application?
- What's New in the .NET Framework Version 3.5
- .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
- New C# Features in the .NET Framework 4
- Disable the CrossThreadChecking in .Net Framework v2.0
- How to hide the OK button in the dialog in .Net Compact Framework application?
- Running multiple versions of the Framework in ASP.NET
- Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application