Home  >  Article  >  Java  >  Java multi-threaded programming read-write lock ReadWriteLock usage example

Java multi-threaded programming read-write lock ReadWriteLock usage example

高洛峰
高洛峰Original
2017-01-05 16:00:331545browse

Read-write lock: It is divided into read lock and write lock. Multiple read locks are not mutually exclusive. Read locks and write locks are mutually exclusive. This is controlled by the jvm itself. You only need to lock the corresponding lock. If your code only reads data, and many people can read it at the same time, but cannot write at the same time, then apply a read lock; if your code modifies data, only one person can write, and cannot read at the same time, then apply a write lock. . In short, the read lock is used when reading, and the write lock is used when writing!

Three threads read data, three threads write data example:
Can read at the same time, cannot write while reading, cannot write at the same time, cannot read while writing.
The read lock is set when reading and unlocked after reading; the write lock is set when writing and unlocked after writing.
Note that it is finally unlocked.

package com.ljq.test.thread;
  
import java.util.Random;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
  
/**
 * 读写锁
 *
 * @author Administrator
 *
 */
public class ReadWriteLockTest {
  public static void main(String[] args) {
    final ReadWrite rw = new ReadWrite();
    for (int i = 0; i < 3; i++) {
      new Thread() {
        public void run() {
          while (true) {
            rw.read();
          }
        }
  
      }.start();
  
      new Thread() {
        public void run() {
          while (true) {
            rw.write(new Random().nextInt(10000));
          }
        }
  
      }.start();
    }
  
  }
}
  
/**
 * 读和写要互斥,因此要把它们放在同一个类中
 *
 * @author Administrator
 *
 */
class ReadWrite {
  private Object data = null;//共享数据,只能有一个线程写该数据,但可以有多个线程同时读该数据。
  ReadWriteLock rwl = new ReentrantReadWriteLock();
  
  /**
   * 读数据
   */
  public void read() {
      
    rwl.readLock().lock();
    try {
      System.out.println(Thread.currentThread().getName() + " be ready to read data!");
      Thread.sleep((long) (Math.random() * 1000));
      System.out.println(Thread.currentThread().getName() + "have read data :" + data);
    } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {
      rwl.readLock().unlock();
    }
      
  }
  
  /**
   * 写数据
   *
   * @param data
   */
  public void write(Object data) {
      
    rwl.writeLock().lock();
    try {
      System.out.println(Thread.currentThread().getName() + " be ready to write data!");
      Thread.sleep((long) (Math.random() * 1000));
      this.data = data;
      System.out.println(Thread.currentThread().getName() + " have write data: " + data);
    } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {
      rwl.writeLock().unlock();
    }
  
  }
}

Design a caching system
Cache system: If you want to get data, you need to call my public Object getData(String key) method. I want to check whether I have this data internally, and if so, I will return it directly. , if not, search the number from the database, and store the data in my internal memory. Next time someone comes to ask for the data, I will directly return the number without looking for it in the database. If you want to get data, don't look for a database, come to me.

package com.ljq.test.thread;
  
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
  
/**
 * 设计一个缓存系统
 *
 *
 * @author Administrator
 *
 */
public class CacheDemo {
  
  private Map<String, Object> cache = new HashMap<String, Object>();
  
  public static void main(String[] args) {
    String key = "name";
    CacheDemo cacheDemo = new CacheDemo();
    System.out.println(cacheDemo.getData(key)); //从数据库获取数据
    System.out.println(cacheDemo.getData(key)); //从缓存获取数据
    System.out.println(cacheDemo.getData(key)); //从缓存获取数据
  }
  
  private ReadWriteLock rwl = new ReentrantReadWriteLock();
  
  public Object getData(String key) {
    rwl.readLock().lock(); //上读锁
    Object value = null;
    try {
      value = cache.get(key); //先查询内部存储器中有没有要的值
      if (value == null) { //如果没有,就去数据库中查询,并将查到的结果存入内部存储器中
        //释放读锁、上写锁
        rwl.readLock().unlock();
        rwl.writeLock().lock();
        try {
          if (value == null) { //再次进行判断,防止多个写线程堵在这个地方重复写
            System.out.println("read data from database");
            value = "张三";
            cache.put(key, value);
          }
        } finally {
          //设置完成 释放写锁
          rwl.writeLock().unlock();
        }
        //恢复读写状态
        rwl.readLock().lock();
      }else{
        System.out.println("read data from cache");
      }
    } finally {
      rwl.readLock().unlock(); //释放读锁
    }
    return value;
  }
}

For more articles related to Java multi-threaded programming read-write lock ReadWriteLock usage examples, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn