Java Multithreading (vii) increases the efficiency of locks-using read-write locks

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/xingjiarong/article/details/47947515

Before we talked about Reentrantlock, this kind of lock does not differentiate between read and write operations, and if one thread is performing a read operation, then all other threads cannot perform any read or write operations. This can guarantee the program's mutex, but reduce the concurrency of the program, so that the execution efficiency is reduced, no effective use of multi-threaded advantages. For example, there is a system, mainly read operations, such as 10 threads are responsible for reading data, only one thread is responsible for writing data. If you use our previous Reentrantlock method, none of the two threads in these 10 threads can be executed concurrently, but let's think about it, if a thread is performing a read operation, can other read threads execute? Of course yes, because the read operation does not change the data, naturally does not cause data errors, so in this case, Java has a reentrantreadwritelock, to control read and write operations.


This type of lock is divided into two types: Read lock and Write lock. If a thread obtains a read lock, the other thread that performs the read operation can continue to acquire the read lock, which means that the read operation can be executed concurrently, but the other write locks will be blocked. If a thread obtains a write lock, any other threads that attempt to acquire read and write locks are blocked.


Let's look at an example:

Import Java.util.concurrent.locks.lock;import Java.util.concurrent.locks.reentrantreadwritelock;public class ReadWrite {private Reentrantreadwritelock lock = new Reentrantreadwritelock ();p rivate lock Readlock;private Lock writelock;private int num = -1;public ReadWrite () {readlock = Lock.readlock (); writelock = Lock.writelock ();} public void Read () {while (true) {Try{readlock.lock (); System.out.println (Thread.CurrentThread () + "ready to read Data!"); Thread.Sleep (1000); System.out.println (Thread.CurrentThread () + "read data is" +num);} catch (Exception e) {e.printstacktrace ();} Finally{readlock.unlock ();}}} public void Write () {while (true) {Try{writelock.lock (); System.out.println (Thread.CurrentThread () + "ready to write Data"); Thread.Sleep (+); num = (int) (Math.random () *10); System.out.println (Thread.CurrentThread () + "data already written" +num); catch (Exception e) {e.printstacktrace ();} Finally{writelock.unlock ();}}}


public class Main {public static void main (string[] args) {final ReadWrite rd = new ReadWrite (), for (int i=0;i<3;i++) {NE W Thread (New Runnable () {@Overridepublic void run () {rd.read ();}}). Start ();;} New Thread (New Runnable () {@Overridepublic void run () {rd.write ();}}). Start ();}}



A reentrantreadwritelock and an int variable are declared in the ReadWrite class, with two methods Readlock () and Writelock () in Reentrantreadwritelock. Used to obtain read and write locks respectively. The ReadWrite class has read and write, respectively, to read the value of NUM and to change the value of Num. In the main program, three threads are responsible for reading the data, and one thread is responsible for writing the data. The results are as follows:



We can clearly see that three read threads are executed concurrently, but the write operation cannot be performed with a read operation. Therefore, you can consider using read-write locks later in the reading and writing operations.


Copyright NOTICE: This article for Bo Master original article, reproduced please indicate the source, view the original article, please visit: Http://blog.csdn.net/xingjiarong

Java Multithreading (vii) increases the efficiency of locks-using read-write locks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.