Examples of the use of thread locks and read write locks in Java multithreaded programming

  • 2020-05-09 18:40:20
  • OfStack

Thread lock Lock
Lock   is equivalent to Synchronized of the current object


import java.util.concurrent.locks.Lock; 
import java.util.concurrent.locks.ReentrantLock; 
/* 
 * Lock lock = new ReentrantLock(); 
 * lock.lock();  lock.unLock(); 
 *  Similar to the  synchronized But not with synchronized  A mixture of  
 */ 
public class LockTest { 
  public static void main(String[] args) { 
    final Outer outer = new LockTest().new Outer(); 
    new Thread(new Runnable() { 
 
      @Override 
      public void run() { 
        // TODO Auto-generated method stub 
        while (true) { 
          outer.out1("zhangxiaoxiao"); 
          outer.out2("lihuoming"); 
        } 
      } 
 
    }).start(); 
    new Thread(new Runnable() { 
 
      @Override 
      public void run() { 
        // TODO Auto-generated method stub 
        while (true) { 
          outer.out1("zhangxiaoxiao"); 
          outer.out2("lihuoming"); 
        } 
      } 
 
    }).start(); 
 
  } 
  class Outer { 
    Lock lock = new ReentrantLock(); 
    void out1(String name) { 
      lock.lock(); 
      int len = name.length(); 
      for (int i = 0; i < len; i++) { 
        System.out.print(name.charAt(i)); 
      } 
      System.out.println(); 
      lock.unlock(); 
    } 
    void out2(String name) { 
      lock.lock(); 
      int len = name.length(); 
      for (int i = 0; i < len; i++) { 
        System.out.print(name.charAt(i)); 
      } 
      System.out.println(); 
      lock.unlock(); 
    } 
  } 
} 


Read-write lock ReentrantReadWriteLock

  read-write lock: allows multiple threads to read simultaneously; You can't write while you're reading; Cannot read while writing; You can't write while you're writing


import java.util.HashMap; 
import java.util.Map; 
import java.util.concurrent.locks.ReadWriteLock; 
import java.util.concurrent.locks.ReentrantReadWriteLock; 
/* 
 *  Question: design 1 a   Caching mechanisms   the   Pseudo code  
 *   Read the data from the set, read it, write it, and consider the case of multi-threading   
 */ 
public class ReadWriteLockTest2 { 
  private Map<String, Object> data = new HashMap<String, Object>(); 
  private ReadWriteLock lock = new ReentrantReadWriteLock();  
  // Read-write lock  
  public Object getInstance1(String key, Object obj) { 
    lock.readLock().lock(); 
    Object value = null; 
    try { 
      value = data.get(key); 
      if (value == null) {//1 A thread goes in here,  
        lock.readLock().unlock();// If the read lock is unlocked, other threads might be here  
        lock.writeLock().lock();// There can only be 1 The thread is here, the write lock is open, and the data is ready to be written  
        if (data.get(key) == null) { 
          value = obj;   
            /* Judge the data first, if it does not exist, then write, because   Bound when the write lock is lifted after the other  
               The thread, when it wants to write,   The data exists   I'm going to skip it. You don't have simultaneous writes  
            */    
          data.put(key, value);  
        } 
        lock.writeLock().unlock();// Done. Unlock  
        lock.readLock().lock(); 
      } 
    } finally { 
      lock.readLock().unlock(); 
    }   
    return value; 
  } 


Related articles: