The Reentrantlock class has a completely mutex exclusive effect, that is, only one thread at a time executes the task after the Reentrantlock.lock () method. While this ensures thread safety for instance variables, it is inefficient. So a read-write lock Reentrantreadwritelock class is provided in the JDK, which can be used to speed up the operation, and in some methods that do not need to manipulate instance variables, You can use read-write lock Reentrantreadwritelock to improve the code speed of the method.
Read-write locks also have two locks, one is read-related locks, also known as shared locks, and the other is write-related locks, also called exclusive locks. 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 are no thread threads for a write-in operation, the read lock can be obtained by more than one thread of the read operation, while the thread of 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.
1. Read and read share
public class Service { private reentrantreadwritelock lock = new Reentrantreadwritelock (); public void Read () { try { lock.readlock (). Lock (); SYSTEM.OUT.PRINTLN ("Get read Lock:" +thread.currentthread (). GetName () + "" +system.currenttimemillis ()); finally { lock.readlock (). Unlock ();}} }
Two Custom threads
public class MyThread1 extends Thread { Private service service; Public MyThread1 (Service service) { this.service = service; } @Override public Void Run () { service.read (); }}
public class MyThread2 extends thread{ Private service service; Public MyThread2 (Service service) { this.service = service; } @Override public Void Run () { service.read (); }}
public class Run {public static void Main (string[] args) throws interruptedexception { Service service = new Serv Ice (); MyThread1 a = new MyThread1 (service); A.setname ("AA"); A.start (); MyThread2 B = new MyThread2 (service); B.setname ("BB"); B.start (); }}
Get read lock: AA 1462676138838 Get read lock: BB 1462676138841 |
When printed from the console, two threads are almost at the same time the code behind the lock method. Note Using the Lock.readlock () read lock here can improve the efficiency of the program. Allows multiple threads to execute the code after the locks method at the same time.
2. Write and write mutually exclusive
public class Service { private reentrantreadwritelock lock = new Reentrantreadwritelock (); public void Write () { try { lock.writelock (). Lock (); System.out.println ("Get write Lock:" +thread.currentthread (). GetName () + "" +system.currenttimemillis ()); finally { lock.writelock (). Unlock ();}} }
The above custom thread changes the Read method to the Write method. The run class does not change, the result is as follows
Get write Lock: BB 1462676627323 Get write Lock: AA 1462676627323 |
The effect of using write lock Lock.writelock () is that only one thread is allowed to execute the code behind the lock method at the same time
3. Write/read/write mutual exclusion
public class Service { private reentrantreadwritelock lock = new Reentrantreadwritelock (); public void Read () { try { lock.readlock (). Lock (); SYSTEM.OUT.PRINTLN ("Get read Lock:" +thread.currentthread (). GetName () + "" +system.currenttimemillis ()); Thread.Sleep (+); } catch (Interruptedexception e) { e.printstacktrace (); } finally { lock.readlock (). Unlock (); } } Public Void Write () { try { lock.writelock (). Lock (); System.out.println ("Get write Lock:" +thread.currentthread (). GetName () + "" +system.currenttimemillis ()); Thread.Sleep (+); } catch (Interruptedexception e) { e.printstacktrace (); } finally { lock.writelock (). Unlock (); } }
The run in the custom thread MyThread1 is the Read method, and the Run method in MyThread2 is write.
Get write Lock: BB 1462677688372 Get read lock: AA 1462677691372 |
Description ' read-write ' is mutually exclusive
"Read-write", "write-read", "write" are mutually exclusive, while "reading" is asynchronous, non-mutex
Java multithreaded Programming 4--reentrantreadwritelock use (read-write lock)