Home >Java >Javagetting Started >What is a read-write lock
We know that there is no problem with multiple threads reading a resource class at the same time, so in the case of concurrency, reading shared resources should be possible at the same time; however, if one If a thread wants to write to a shared resource, there should be no other threads reading or writing to the shared resource at the same time.
(Recommended tutorial: java introductory tutorial)
What we want is: allow multiple threads to read at the same time, but as long as one thread is writing, other threads will Have to wait.
Read-write lock is based on this principle, that is, read-write lock can allow multiple multi-thread access at the same time, but when the write thread accesses, all read threads and Other writing threads will be blocked. Read-write locks actually maintain a pair of locks, one read lock and one write lock. By separating read locks and write locks, its concurrency is greatly improved compared to exclusive locks (exclusive locks).
Code example:
public class ReadWriteLockDemo { public static void main(String[] args) { ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); Book book = new Book();//创建一本书,可读可写 //两个写手 for (int i = 0; i < 2; i++) { int num = i; new Thread(()->{ try { readWriteLock.writeLock().lock(); System.out.println("写手"+num+"在写文章"); book.write(String.valueOf(num),UUID.randomUUID().toString().substring(0,5)); }catch (Exception e){ e.printStackTrace(); }finally { readWriteLock.writeLock().unlock(); } },String.valueOf(i)).start(); } //6个读者 for (int i = 0; i < 6; i++) { int num = i; new Thread(()->{ try { readWriteLock.readLock().lock(); String word = book.read(String.valueOf(num % 2)); System.out.println("读者"+num+"在阅读文章..."+word); } catch (Exception e) { e.printStackTrace(); } finally { readWriteLock.readLock().unlock(); } },String.valueOf(i)).start(); } } } class Book{ HashMap<String, String> map = new HashMap<>(); public void write(String key,String val){ map.put(key, val); } public String read(String key){ String word = map.get(key); return word; } }
Output result:
"C:\Program Files\Java\jdk1.8.0_144\bin\java.exe" "-javaagent:F:\MyDir\IDEA\IDEA2018\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=54141:F:\MyDir\IDEA\IDEA2018\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;E:\idea_workplace\javaBase\target\classes" 写手0在写文章 写手1在写文章 读者0在阅读文章...6b021 读者1在阅读文章...220e4 读者2在阅读文章...6b021 读者4在阅读文章...6b021 读者3在阅读文章...220e4 读者5在阅读文章...220e4 Process finished with exit code 0
Efficiency of read-write lock
(Video tutorial recommendation: java video tutorial)
Whether read-write locks will improve performance using mutex locks depends on how often data is read and modified, the duration of read and write operations, and contention for the data. Try The number of threads reading or writing data simultaneously.
For example, a collection that is initially populated with data and then frequently searched (such as a directory of some kind) that is often modified is an ideal candidate for using a read-write lock. However, if updates become frequent, the data will be locked exclusively most of the time, with little increase in concurrency.
Additionally, if the read operation is too short, the overhead of a read-write lock implementation (which is itself more complex than a mutex) can dominate the execution cost, especially since many read-write lock implementations will still serialize all Threads pass through small portions of code. Ultimately, only profiling and measurement will determine whether using read-write locks is appropriate for your application.
The above is the detailed content of What is a read-write lock. For more information, please follow other related articles on the PHP Chinese website!