Readwritelock read-write lock implements thread read-write mutex problem

Source: Internet
Author: User
Tags mutex sleep
It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.
1.Lock is more object-oriented than the traditional threading model of synchronized, and the lock itself should be an object, similar to a lock in life. Code fragments executed by two threads to achieve the effect of a synchronous mutex, they must use the same lock object.

2. Read and write Lock: divided into read lock and write lock, multiple read lock is not mutually exclusive, read lock and write lock mutually exclusive, this is controlled by the JVM itself, you only need to good the corresponding lock. If your code is read-only and can be read by a lot of people at the same time, but not at the same time, read the lock; If your code modifies the data, only one person is writing and cannot read at the same time, write the lock. In short, read the time to read the lock, write the time to write the lock.

Example program:

Package edu.java5.lock;
Import Java.util.Random;
Import Java.util.concurrent.locks.Lock;
Import Java.util.concurrent.locks.ReentrantLock;

Import Java.util.concurrent.locks.ReentrantReadWriteLock;
     public class Readwritelock {/** * Read and write locks: divided into read and write locks, multiple read locks are not mutually exclusive, read lock and write lock mutex, which is controlled by the JVM itself, you just need to good the corresponding lock.
     * If your code is read-only and can be read by many people at the same time, but not at the same time, read the lock; * If your code modifies the data, only one person is writing and cannot read at the same time, write the lock. 
     * In short, read the time to read the lock, write the time on the write lock.
       * Topic Requirements: * Three threads read data, three threads write data, write data can not read data, read data while other threads can read data but not write data; **/public static void Main (string[] args) {
       Final Dataoperate dataoperate = new Dataoperate ();
				  for (int i = 0; i < 3; i++) {New Thread (new Runnable () {@Override public void run () {while (true) {
				Dataoperate.get ();
		  }}). Start (); New Thread (New Runnable () {@Override public void run () {while (true) {Dataoperate.put (New Random (). Nextint
				(1000));
	   }}). Start (); }}}} class dataoperate{Object data = null;//shared data, only one thread can write that data, but there can beMultiple threads read the data reentrantreadwritelock lock = new Reentrantreadwritelock ();
		public void Get () {Lock.readlock (). Lock ();
		System.out.println (Thread.CurrentThread (). GetName () + "ready to read data ...");
		try {thread.sleep (1000);
		} catch (Interruptedexception e) {e.printstacktrace ();		
        } System.out.println (Thread.CurrentThread (). GetName () + "read data" +data);
	Lock.readlock (). Unlock ();
        } public void put (int data) {Lock.writelock (). Lock ();		
        System.out.println (Thread.CurrentThread (). GetName () + "prepare to write data");
		try {thread.sleep (1000);
		} catch (Interruptedexception e) {e.printstacktrace ();
		} this.data = data;
		System.out.println (Thread.CurrentThread (). GetName () + "already written data" +data);
	Lock.writelock (). Unlock (); }
	
}


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.