The Monitor class describes a mechanism for synchronizing access to objects.
1. Located under the System.Threading namespace, the mscorlib.dll assembly.
2.Monitor realizes multi-threaded synchronization problem by acquiring and releasing the exclusive lock.
3.Monitor implements multi-threaded synchronization within the current process, similar to the functionality of the lock statement.
4. Currently static class, easy to use
5. The dependent lock object is similar to the lock statement, which also requires reference type, recommended private, read-only, static
Define the code:
// // Summary: // provides a mechanism for synchronizing access to objects. [ComVisible (true)] publicstaticclass Monitor
Ii. Description of common methods
The 1.Enter (obj) method obtains an exclusive lock on the specified object.
2.Exit (obj) releases an exclusive lock on the specified object.
3.IsEntered (obj) to determine if the current thread has held an exclusive lock
4.TryEnter (Object obj, TimeSpan timeout) attempts to acquire an exclusive lock on the specified object within a specified amount of time.
5.Wait (Object obj) frees the lock on the object and blocks the current thread until it acquires the lock again.
6.Pulse (Object obj) notifies a thread in the waiting queue of a change in the state of the locked object.
7.PulseAll (Object obj) notifies all waiting thread object state changes.
Iii. Example Description One:
Description: Multi-threaded cumulative values to resolve synchronization issues
Private ReadOnly Static Object_mylock =New Object();Static intCount =0;Static voidCountadd () {monitor.enter (_mylock);//Get exclusive lockcount++; Console.WriteLine (Count); if(Count = =Ten) {Count=0; } monitor.exit (_mylock); //release the exclusive lock} Public Static voidTestone () {//Start 4 threads, accumulate count for(inti =0; I <4; i++) {Task.Factory.StartNew ()= { while(true) {countadd (); Thread.Sleep ( -*i); } }); }}
1. If you do not use exclusive lock processing, there will be data exceptions
2. Using the lock result is normal
Iv. locking processing with the use () {} block
1. This scenario may be used in transactions, to guarantee the same transaction, and if one thread is open, the other threads wait for the current transaction operation.
2. This scenario for external use, do not need to consider multithreading issues
1. The encapsulation of the IDisposable interface is inherited
classaddhelper:idisposable {Private ReadOnly Static Object_mylock =New Object(); Static intCount =0; PublicAddhelper () {//Enable exclusive lockMonitor.Enter (_mylock); } Public voidAddcount () {Count++; Console.WriteLine (Count); if(Count = =Ten) {Count=0; } } Public voidDispose () {//release the exclusive lockMonitor.Exit (_mylock); } }
2. Calling code blocks
Public Static voidTesttwo () {//Start 4 threads, accumulate count for(inti =0; I <4; i++) {Task.Factory.StartNew ()= { while(true) { //if exclusive lock processing is not used, a data exception occurs//addhelper _add = new Addhelper (); //_add. Addcount (); //encapsulating an exclusive lock processing using(Addhelper _add =NewAddhelper ()) {_add. Addcount (); } thread.sleep ( -*i); } }); }}
For more information:
C # lock Keyword/lock statement block, Thread lock
using the C # using keyword collation
transaction operations in Entiryframework (ii)
Official reference: Https://msdn.microsoft.com/zh-cn/library/system.threading.monitor.aspx
C # thread Sync exclusive lock/monitor Monitor class