The class Reentranlock has the effect of a universal mutex exclusive, that is, only one thread at a time executes the task behind the Reentrantlock.lock () method. This guarantees the thread safety of the instance variable,
But the efficiency is very low. So a read-write lock Reentrantreadwritelock class is provided in the JDK, which makes it faster to run, and in some methods that do not require the manipulation of instance variables, a read-write lock can be used completely
Reentrantreadwritelock to increase the speed of the code for this method.
The read-write lock indicates that there are also two locks, one is a read operation-related lock, also known as a shared lock, and the other is write-related to do, also called an exclusive lock. That is, multiple read locks are not mutually exclusive, the read lock is mutually exclusive to the write lock, and the write lock is mutually exclusive.
when there is no out-of-the-box thread for a write operation, the read lock can be acquired by multiple thread for the read operation, and the thread for the write operation cannot write until the write lock is acquired. That is, multiple thread can read at the same time, but only one thread is allowed to write at the same time .
Read-read sharing:
Service class:
Package Readandwritelock;import Java.io.bufferedwriter;import Java.time.localdate;import Java.util.concurrent.locks.reentrantlock;import Java.util.concurrent.locks.reentrantreadwritelock;public Class Service {private Reentrantreadwritelock lock=new reentrantreadwritelock ();p ublic void Read () {try {try {lock.readlock ()} . Lock ();; System.out.println ("Thread:" +thread.currentthread (). GetName () + " Time:" +system.currenttimemillis () + " get Lock"); Thread.Sleep (10000);} finally {Lock.readlock (). Unlock ();}} catch (Exception e) {e.printstacktrace ();}}}
Main method:
Package Readandwritelock;public class Run {public static void main (string[] args) {Final Service service=new service (); new Thread (New Runnable () {public void run () {Service.read ()}}). Start (); New Thread (New Runnable () {public void run () {Service.read ()}}). Start ();}}
Console:
Threads: Thread-0 time: 1534585340658 get lock thread: Thread-1 time: 1534585340659 get lock
It can be found that two threads almost at the same time enter the code behind the lock method, stating that using read-write locks here can provide a program to run efficiently, allowing multiple threads to simultaneously develop code behind the lock () method.
Write-Write Mutex:
Service class
public class Service1 {private Reentrantreadwritelock lock=new reentrantreadwritelock ();p ublic Void Write () {try {try {lo Ck.writelock (). lock ();; System.out.println ("Thread: " +thread.currentthread (). GetName () + "Time: " +system.currenttimemillis () + " get lock "); Thread.Sleep (10000);} finally {Lock.writelock (). Unlock ();}} catch (Exception e) {e.printstacktrace ();}}}
Main thread:
public class Run {public static void main (string[] args) {final Service1 service1=new Service1 (); New Thread (New Runnable () {public void Run () {service1.write ();}}). Start (); New Thread (New Runnable () {public void run () {Service1.write ()}}). Start ();}}
Console:
Threads: Thread-0 time: 1534585587952 get lock thread: Thread-1 time: 1534585597955 get lock
The effect of using the write lock Code Lock.writelock () is that the consent time allows only one thread to execute the code following the lock () method.
Write Read mutex:
Service2 class
public class Service2 {private Reentrantreadwritelock lock=new reentrantreadwritelock ();p ublic void Read () {try {try {loc K.readlock (). Lock (); System.out.println ("Thread:" +thread.currentthread (). GetName () + " Time:" +system.currenttimemillis ()); Thread.Sleep (10000);} finally {Lock.readlock (). Unlock ();}} catch (Exception e) {e.printstacktrace ();}} public void Write () {try {try {lock.writelock (). Lock (); System.out.println ("Thread:" +thread.currentthread (). GetName () + " Time:" +system.currenttimemillis ()); Thread.Sleep (10000);} finally {Lock.writelock (). Unlock ();}} catch (Exception e) {e.printstacktrace ();}}}
Main thread:
public class Run {public static void main (string[] args) {final Service2 service2=new Service2 (); New Thread (New Runnable () {public void Run () {service2.write ();}}). Start (); New Thread (New Runnable () {public void run () {Service2.read ()}}). Start ();}}
Console:
Threads: Thread-0 Time: 1534586198910 Threads: Thread-1 Time: 1534586208910
In terms of time, write-read operations are mutually exclusive.
Read-Write Mutex:
Change the method in the main thread to the following:
public class Run {public static void main (string[] args) throws Interruptedexception {final Service2 service2=new Service2 (); New Thread (New Runnable () {public void run () {Service2.read ()}}). Start (); Thread.Sleep (+); new Thread (New Runnable () {public void run () {Service2.write ()}}). Start ();}}
Console:
Threads: Thread-0 Time: 1534586426809 Threads: Thread-1 Time: 1534586436810
Read-write operations are also mutually exclusive.
Conclusion: Reading, writing and writing are mutually exclusive: while Read and read are asynchronous, non-mutually exclusive.
Every good person has a silent time. No complaining, no complaints, finally through the passage of the time to move their own days.
Multi-threaded Learning read-write lock