One of the most important issues in the synchronization problem is the synchronization domain, what is the domain of synchronization? Simply by synchronized the keyword, it is the range that it synchronizes. Many of the problems in concurrent programming are caused by not selecting the synchronization range. But the existing synchronization keyword synchronized can be reflected in the control of the domain, it is estimated that the use of people are not very ideal. This time is not very nostalgic for the mutex operation provided under Windows.
Now for the convenience of our own can also do a Windows-like mutex class to facilitate our control of the domain, the following look at the production process of this class.
As with the previous class, let's start with what we want this class to do:
1. Call Getmutexflag (), only one of the concurrent threads can continue execution, the rest of the block.
2. Call Freemutexflag (), the executing thread leaves the synchronization block (the critical section), and can release a thread from the blocking thread to return from Getmutexflag ().
3.getMutexFlag (), Freemutexflag (), must be atomic operations.
The above three conditions are in fact a requirement for a mutex under Windows.
Already started we might write this code:
1 Package Com.choi;2 3 Public classMutexflag {4 5 protectedThread CurrentThread =NULL;6 7 PublicSynchronizedvoidGetmutexfalg () {8 if(CurrentThread = =NULL) {9CurrentThread =Thread.CurrentThread ();Ten } One while(CurrentThread! =Thread.CurrentThread ()) { A Try { - wait (); - if(CurrentThread = =NULL) { theCurrentThread =Thread.CurrentThread (); - } -}Catch(Exception e) { - } + } - } + A PublicSynchronizedvoidFreemutexflag () { at if(currentthread!=NULL){ -CurrentThread =NULL; - Try { - notify (); -}Catch(Exception e) { - } in } - } to}
This code seems to be no problem, in fact, this piece of code is really no problem to run, but can we make it available to our project team? The answer is no, let's consider a situation:
The project team has three programmers a,b,c a function written in this class (get to mutex is not freed), B writes a function with this class (get to mutex is not freed), C calls A and B functions, is used in a thread. When this thread runs to a, it gets a mutex, and then goes to B to apply for a mutex to apply for it? Look at the code, the answer is no. And can't apply because the first one hasn't been released yet. In Java if it is two synchronized nested, then no problem, the first to get a lock, the second if the application is the same lock, it will automatically determine that the application has been applied.
We can use a variable to be the nested solution for our Mutexflag implementation synchronized:
1 Package Com.choi;2 3 Public classMutexflag {4 5 protectedThread CurrentThread =NULL;6 protected intCount =0;7 8 PublicSynchronizedvoidGetmutexfalg () {9 while(Trygetmutexflag () = =false) {Ten Try { One wait (); A}Catch(interruptedexception e) { - e.printstacktrace (); - } the } - } - - Publicsynchronized Boolean trygetmutexflag () { + if(CurrentThread = =NULL) { -CurrentThread =Thread.CurrentThread (); +Count =1; A return true; at } - if(CurrentThread = =Thread.CurrentThread ()) { -count++; - return true; - } - return false; in } - to PublicSynchronizedvoidFreemutexflag () { + if(CurrentThread = =Thread.CurrentThread ()) { -count--; the } * if(Count = =0) { $CurrentThread =NULL;Panax Notoginseng notify (); - } the } +}
In order to write convenient I put the judgment to get Mutexflag code written to the synchronization of the Trygetmutexflag, did not apply to count to do a single operation, each free of count do a minus one operation, So when count is new equals zero, we go to release a thread to get mutexflag.
Oh, some people say that if you call free without calling get, there is a problem here, because if there is no get,currentthread is equal to null, so count==0, then will call notify, actually no problem, test found that Notify does not throw a human exception, which is equivalent to an empty operation.
Welcome to my GitHub home page get code: Https://github.com/choitony/Java-MutexFlag-Class/tree/master
Self-made mutex classes in Java