Java attack C # -- thread synchronization of syntax,
In the previous chapter, we talked about the application of the C # thread direction. But I didn't talk about another point in multithreading-synchronization. Dirty data may occur in multi-threaded application development. More or less synchronous functions are used. This chapter describes the thread synchronization issues. According to the author's. NET development over the past few years, there are at least four synchronous methods. For example. Lock, volatile, and Monitor.
The lock keyword is similar to the synchronized keyword in JAVA. But there are some differences. The synchronized keyword of JAVA may be modified on the method. Unfortunately, C # cannot be modified on the method. Is the usage different. Let's take a look at the code?
C #:
lock (aomiLock){ Thread.Sleep(300000); i++; Console.WriteLine(aomiLock.Name);}
The above aomiLock is just an instance of the class defined by the author. Obviously, I cannot see the differences in writing. A newbie asked me about lock usage: If I have two static global variables. A parameter value used for lock. That is, the location where the above aomiLock is modified. The other is inside the lock. It is in braces. Can I modify the internal lock value elsewhere? I can only say sorry to him. It can be modified.
Note: the fact that the volatile keyword is mentioned above also has the volatile keyword in JAVA. All apply to the variable level. The function is to make the changes visible. This is the same as JAVA. If you want to use the same submodifier as the synchronized keyword on the method. Sorry, please use [MethodImpl (MethodImplOptions. Synchronized)].
The usage of the Monitor class is similar to the locak keyword. The only difference is that the Monitor class provides too many flexible functions. Do you know a Lock interface provided after JAVA 5.0. Its instance type ReentrantLock. Its functions are similar to those of the Monitor class.
Bool acquireLock = false; Monitor. enter (aomiLock, ref acquireLock); // content I ++; Console. writeLine ("add I =" + I); Monitor. exit (aomiLock );
This is a mutex lock, which is rarely used for synchronization. The role of the Mutex class can be between processes. Therefore, it is used to run only one application on a computer. In addition, he used a large amount of resources. However, I cannot prove it. In short, the author rarely uses him.
1 class Program 2 { 3 public static Mutex mutex; 4 static void Main(string[] args) 5 { 6 bool created; 7 mutex = new Mutex(false, "aomiApplication", out created); 8 9 Console.ReadKey();10 11 }12 13 public static void Add()14 {15 for (int i = 0; i < 10; i++)16 {17 mutex.WaitOne();18 Console.WriteLine("add i=" + i);19 mutex.ReleaseMutex();20 21 }22 23 }24 }
The first parameter of the Mutex constructor is used for representation. Whether to obtain the Mutex right when creating a Mutex class. True indicates immediate access. If this parameter is set to false, the WaitOne method is used. I believe the author does not need to say that everyone understands.
SemaphoreSlim class I think it is a class of the control line. It is often used to control the maximum number of threads or asynchronous running in the current application. I will apply it more when designing the company's framework.
Class Program {public static SemaphoreSlim ss = new SemaphoreSlim (4); static void Main (string [] args) {for (int I = 0; I <10; I ++) {Thread thread = new Thread (Add); thread. start ();} Console. readKey ();} public static void Add () {if (ss. wait (2000) {Console. writeLine ("running"); Thread. sleep (1, 2000); ss. release ();} else {Console. writeLine ("busy thread ");}}}
I personally think this class does not belong to the synchronization class. I think it is a concept of a signal lamp. However, it can also achieve synchronization of small functions. I often use it when designing a framework.
Class Program {public static AutoResetEvent mainEvent = new AutoResetEvent (false); static void Main (string [] args) {Thread thread = new Thread (Execute); thread. start (); mainEvent. set (); // notify him that the Console can be executed. readKey ();} public static void Execute () {mainEvent. waitOne (); Console. writeLine ("Execute is running"); // content }}
This chapter describes some common methods for synchronizing data between threads. Of course, the above is just what I often use. Not comprehensive. Now, this chapter is here.