Reentrantlock
Reentrantlock, a reentrant mutex that has the same basic behavior and semantics as the implicit monitor lock accessed using the Synchronized method and the statement, but is more powerful.
Reentrantlock Basic Usage
Let's take a look at the basic usage of Reentrantlock:
Public classthreaddomain38{PrivateLock lock =NewReentrantlock (); Public voidTestMethod () {Try{lock.lock (); for(inti = 0; I < 2; i++) {System.out.println ("ThreadName =" + Thread.CurrentThread (). GetName () + ", I =" +i); } } finally{lock.unlock (); } }}
public class MyThread38 extends thread{ private ThreadDomain38 TD; public MYTHREAD38 (ThreadDomain38 TD) { TD; public void run () {Td.testmethod (); }}
Public Static void Main (string[] args) { new ThreadDomain38 (); New MYTHREAD38 (TD); New MYTHREAD38 (TD); New MYTHREAD38 (TD); Mt0.start (); Mt1.start (); Mt2.start ();}
Look at the results of the operation:
ThreadName = Thread-1, I = 0= Thread-1, i = 1= Thread-0, I = 0= Thread-0, i = 1 = Thread-2, I = 0= Thread-2, I = 1
Without any alternation, the data is printed in groups, indicating that the next thread can get the lock to print the data after a thread has been printed, which proves that the Reentrantlock has a lock function.
Reentrantlock is holding an object monitor.
The Reentrantlock has been proved to have a lock function, but we do not know what locks Reentrantlock hold, so write an example to see:
Public classthreaddomain39{PrivateLock lock =NewReentrantlock (); Public voidMethodA () {Try{lock.lock (); System.out.println ("MethodA Begin ThreadName =" +Thread.CurrentThread (). GetName ()); Thread.Sleep (5000); System.out.println ("MethodA End threadname =" +Thread.CurrentThread (). GetName ()); } Catch(interruptedexception e) {e.printstacktrace (); } finally{lock.unlock (); } } Public voidMethodB () {Lock.lock (); System.out.println ("MethodB Begin ThreadName =" +Thread.CurrentThread (). GetName ()); System.out.println ("MethodB Begin ThreadName =" +Thread.CurrentThread (). GetName ());Lock.unlock (); }}
Write two threads to call the MethodA () and MethodB () methods respectively:
Public class extends thread{ Private ThreadDomain39 td; Public Mythread39_0 (ThreadDomain39 TD) { this. td = TD; } Public void Run () { Td.methoda (); }}
Public class extends thread{ Private ThreadDomain39 td; Public Mythread39_1 (ThreadDomain39 TD) { this. td = TD; } Public void Run () { td.methodb (); }}
Write a main function to start these two threads:
Public Static void Main (string[] args) { new ThreadDomain39 (); New Mythread39_0 (TD); New Mythread39_1 (TD); Mt0.start (); Mt1.start ();}
Look at the results of the operation:
MethodB begin threadname = Thread-1= Thread-1= Thread-0= Thread-0
Can't see the time, but the four really is the lattice 5 seconds or so to print out. From the results, it has been proved that Reentrantlock holds the object monitor, can write a piece of code to further prove this conclusion, that is, to remove the METHODB () internal and lock-related code, leaving only two lines of print statements:
MethodA begin threadname = Thread-0= Thread-1= Thread-1= Thread-0
See alternate printing, further proving that Reentrantlock holds the "object Monitor" conclusion.
But note that although Reentrantlock holds object monitors, the object monitors that are held with synchronized are not meant to be, although I am not sure what the difference is with the two object monitors that are held, but the MethodB () The method is modified with synchronized, MethodA () unchanged, and two methods run asynchronously, so let's take a conclusion----the object monitors held by Reentrantlock and synchronized are different .
In addition, do not forget,Reentrantlock hold the lock is need to manually go to unlock ()
Condition
The synchronized is combined with the wait () and Nitofy ()/notifyall () methods to implement the wait/notify model, and Reentrantlock can do the same, but condition is needed, and condition has better flexibility. Specifically reflected in:
1, a lock inside can create more than one condition instance, realize the multi-channel notification
2, the Notify () method is notified when the thread being notified is randomly selected by the Java Virtual machine, but the reentrantlock combination condition can be implemented selectively to notify, which is very important
Take a look at the simplest use of the condition to implement the wait/notification model, the following code notes that before await () and signal (), you must first lock () to obtain locks, use finished in finally unlock () release lock, this and wait ()/ Notify ()/notifyall () must obtain the same object lock before use:
Public classthreaddomain40{PrivateLock lock =NewReentrantlock (); PrivateCondition Condition =lock.newcondition (); Public voidawait () {Try{lock.lock (); System.out.println (The await time is: "+System.currenttimemillis ()); Condition.await (); System.out.println ("Await wait to end"); } Catch(interruptedexception e) {e.printstacktrace (); } finally{lock.unlock (); } } Public voidsignal () {Try{lock.lock (); System.out.println ("Signal Time:" +System.currenttimemillis ()); Condition.signal (); } finally{lock.unlock (); } }}
public class MyThread40 extends thread{ private THREADDOMAIN40 TD; public MYTHREAD40 (ThreadDomain40 TD) { TD; public void run () {td.await (); }}
Public Static void throws exception{ new ThreadDomain40 (); New MYTHREAD40 (TD); Mt.start (); Thread.Sleep (+); Td.signal ();}
Look at the results of the operation:
Await time: 1443970329524signal time:1443970332524await wait end
The difference is 3000 milliseconds or 3 seconds, conforming to the expectations of the code and successfully using Reentrantlock's condition to implement the wait/notification model. In fact, this example also proves that thecondition await () method is to release the lock , the reason is very simple, if the await () method does not release the lock, then the signal () method can be called to condition signal () Method?
Note that if a condition is used, then when multiple threads are given an await () by the condition, the Signalall () method called condition wakes up all the threads. What if you want to wake up some threads separately? New can be done with multiple condition, which also helps to improve the efficiency of the program's operation. Using multiple condition scenes is very common, as is the case with Arrayblockingqueue.
The use and condition of Java multithreading 11:reentrantlock