(39) read-write lock ReadWriteLock

ReadWriteLock is an interface, in which it defines only two methods:

Used to obtain a read lock, used to obtain a write lock. That is to separate read and write files, divided into two to allocate to the thread lock so that multiple threads can be read simultaneously .

 

public interface ReadWriteLock {

 

    /**

 

     * Returns the lock used for reading.

 

     * @return the lock used for reading.

 

     */

 

    Lock readLock (); // get a read lock

 

    /**

 

     * Returns the lock used for writing.

 

     * @return the lock used for writing.

 

     */

 

    Lock writeLock (); // get a write lock

 

}

Once the thread has received a write lock, other threads can not do the operation; if the thread acquired a read lock, other threads can still be read.

 

The following ReentrantReadWriteLock achieved ReadWriteLock interface.

ReentrantReadWriteLock which provided many ways, but the most important are two methods: readLock () and writeLock () is used to obtain read and write locks.

 

By following a few examples look ReentrantReadWriteLock of the specific usage.

 

Example 1 : If there are multiple threads to be read at the same time, then look at synchronized to achieve results

 

package cn.itcast_01_mythread.thread.lock;

/ **
* read a thread but also have to write, to synchronize with the true, read and write operations can only lock a thread after thread a manner
* @author
*
* /
public class MySynchronizedReadWrite {
       public static void main (String [] args) {
              Final Test MySynchronizedReadWrite new new MySynchronizedReadWrite = ();
              new new the Thread () {
                      public void RUN () {
                             test.get (Thread.currentThread ());
                      };
               } .start ();
              new new the Thread () {
                      public RUN void () {
                             test.get (Thread.currentThread ());
                      };
               } .start ();

       }

       the synchronized void GET public (the Thread Thread) {
              Long Start = System.currentTimeMillis ();
              int I = 0;
              the while (System.currentTimeMillis () - Start <=. 1) {
                     I ++;
                     IF (I%. 4 == 0) {
                            the System .out.println (thread.getName () + "write operation in progress");
                     } the else {
                            System.out.println (Thread.getName () + "being read");
                      }
              }
            System.out.println ( thread.getName () + "write operation is complete");
       }

}

 

Examples 2 : read-write lock was used instead of the words

 

import java.util.concurrent.locks.ReentrantReadWriteLock;

/ **
* Use the read-write lock, the lock can read and write separation, read concurrently, write locks single thread
*
* If there is a thread already occupied read lock, other threads at this time if you want to apply for a write lock, then application write lock thread will wait to release the read lock.
* If there is a thread already occupied write lock, then other threads at this time if the application write lock or read lock, the application thread will wait to release the write lock.
@Author *
*
* /
public class MyReentrantReadWriteLock {
        Private ReentrantReadWriteLock with new new ReentrantReadWriteLock with RWL = ();

        public static void main (String [] args) {
               Final Test MyReentrantReadWriteLock new new MyReentrantReadWriteLock = ();
               new new the Thread () {
                       public void RUN () {
                               test.get (Thread.currentThread ());
                               test.write (Thread.currentThread ());
                       };
                }.start();

              new Thread(){
                      public void run() {
                             test.get(Thread.currentThread());
                             test.write(Thread.currentThread());
                      };
               }.start();

         }

       /**
         * 读操作,用读锁来锁定
         * @param thread
        */
        public void get(Thread thread) {
               rwl.readLock().lock();  //获取读锁
               try {
                    long start = System.currentTimeMillis();
                    while(System.currentTimeMillis() - start <= 1) {
                          System.out.println (thread.getName () + "being read");
                     }
                    System.out.println (thread.getName () + "read complete");
                     } {the finally
                     . Rwl.readLock () UNLOCK ();
                }
         }

        / *
          * Write operation, write locks to lock
          * @param Thread
         * /
         public void Write (the Thread Thread) {
                rwl.writeLock () Lock ();. // obtain a write lock
                the try {
                       Long Start = System.currentTimeMillis ( );
                       the while (System.currentTimeMillis () - Start <=. 1) {
                             System.out.println (Thread.getName () + "write operation in progress");
                        }
                       System.out.println (Thread.getName () + " write operation is complete ");
                   } {the finally
                       rwl.writeLock () UNLOCK ();.
                    }
         }
}

 

Guess you like

Origin www.cnblogs.com/paradis/p/11431967.html