In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "C # how to use Monitor class to achieve thread synchronization", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "C # how to use Monitor class to achieve thread synchronization" bar!
I. brief introduction
The Lock keyword is an alternative use of Monitor, and lock is translated into Monitor in IL code.
Lock (obj) {/ / Code snippet} / / is equivalent to Monitor.Enter (obj); / / Code snippet Monitor.Exit (obj)
Common properties and methods of Monitor:
Enter (Object) acquires an exclusive lock on the specified object.
Exit (Object) releases the exclusive lock on the specified object.
Pulse notifies threads in the waiting queue of changes in the state of the locked object.
PulseAll notifies all waiting threads of a change in the state of the object.
TryEnter (Object) attempts to acquire an exclusive lock for the specified object.
TryEnter (Object, Boolean) attempts to acquire an exclusive lock on the specified object and automatically sets a value indicating whether the lock has been obtained.
Wait (Object) releases the lock on the object and blocks the current thread until it reacquires the lock.
There are two commonly used methods
The Monitor.Enter (object) method is to acquire the lock
The Monitor.Exit (object) method releases the lock
These are the two most commonly used methods of Monitor. In order to avoid that the lock cannot be released because of an exception after acquiring the lock, you need to release the lock (Monitor.Exit ()) in the finally {} structure after try {} catch () {}.
Code 1.Enter (Object) case
The use of Enter (Object) is simple. Look at the code.
Class Program {static void Main (string [] args) {Thread threadA = new Thread (ThreadMethod); threadA.Name = "A"; Thread threadB = new Thread (ThreadMethod); threadB.Name = "B"; threadA.Start (); threadB.Start (); Thread.CurrentThread.Name = "C"; ThreadMethod () Console.ReadKey ();} static object obj = new object (); public static void ThreadMethod () {Monitor.Enter (obj); / / Monitor.Enter (obj) stable object try {for (int I = 1; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.