【并发编程】可重入锁和读写锁

2022-10-25 15:25:35 浏览数 (1)

可重入锁ReentrantLock

何为重入: 重进入是指任意线程在获取到锁之后能够再次获取该锁而不会被锁阻塞

代码语言:javascript复制
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class UseReentrantLock {
	
	private Lock lock = new ReentrantLock();
	
	public void method1(){
		try {
			lock.lock();
			System.out.println("当前线程:"   Thread.currentThread().getName()   "进入method1..");
			Thread.sleep(1000);
			System.out.println("当前线程:"   Thread.currentThread().getName()   "退出method1..");
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			
			lock.unlock();
		}
	}
	
	public void method2(){
		try {
			lock.lock();
			System.out.println("当前线程:"   Thread.currentThread().getName()   "进入method2..");
			Thread.sleep(2000);
			System.out.println("当前线程:"   Thread.currentThread().getName()   "退出method2..");
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			
			lock.unlock();
		}
	}
	
	public static void main(String[] args) {

		final UseReentrantLock ur = new UseReentrantLock();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				ur.method1();
				ur.method2();
			}
		}, "t1");

		t1.start();
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//System.out.println(ur.lock.getQueueLength());
	}
	
	
}

运行结果:

一般使用lock.lock();就try catch 在finally里释放锁:lock.unlock(); 功能和synchronized差不多 比synchronized轻量

在使用synchronized的时候 多线程间进行协作工作需要通过wait() notify() 进行配合工作

使用Lock的时候 使用Condition类来等待和通知现场 Condition针对的是具体某一把锁

Lock Condition 的使用:

代码语言:javascript复制
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class UseCondition {

	private Lock lock = new ReentrantLock();
	private Condition condition = lock.newCondition();
	
	public void method1(){
		try {
			lock.lock();
			System.out.println("当前线程:"   Thread.currentThread().getName()   "进入等待状态..");
			Thread.sleep(3000);
			System.out.println("当前线程:"   Thread.currentThread().getName()   "释放锁..");
			condition.await();	// 相当于Object wait 阻塞 等待signal();
			System.out.println("当前线程:"   Thread.currentThread().getName()  "继续执行...");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public void method2(){
		try {
			lock.lock();
			System.out.println("当前线程:"   Thread.currentThread().getName()   "进入..");
			Thread.sleep(3000);
			System.out.println("当前线程:"   Thread.currentThread().getName()   "发出唤醒..");
			condition.signal();		//相当于 Object notify
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public static void main(String[] args) {
		
		final UseCondition uc = new UseCondition();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				uc.method1();
			}
		}, "t1");
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				uc.method2();
			}
		}, "t2");
		t1.start();

		t2.start();
	}
	
	
	
}

运行结果:

多Condition下Lock

代码语言:javascript复制
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class UseManyCondition {

	private ReentrantLock lock = new ReentrantLock();
	private Condition c1 = lock.newCondition();
	private Condition c2 = lock.newCondition();
	
	public void m1(){
		try {
			lock.lock();
			System.out.println("当前线程:"  Thread.currentThread().getName()   "进入方法m1等待..");
			c1.await();
			System.out.println("当前线程:"  Thread.currentThread().getName()   "方法m1继续..");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public void m2(){
		try {
			lock.lock();
			System.out.println("当前线程:"  Thread.currentThread().getName()   "进入方法m2等待..");
			c1.await();
			System.out.println("当前线程:"  Thread.currentThread().getName()   "方法m2继续..");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public void m3(){
		try {
			lock.lock();
			System.out.println("当前线程:"  Thread.currentThread().getName()   "进入方法m3等待..");
			c2.await();
			System.out.println("当前线程:"  Thread.currentThread().getName()   "方法m3继续..");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public void m4(){
		try {
			lock.lock();
			System.out.println("当前线程:"  Thread.currentThread().getName()   "唤醒..");
			c1.signalAll();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public void m5(){
		try {
			lock.lock();
			System.out.println("当前线程:"  Thread.currentThread().getName()   "唤醒..");
			c2.signal();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public static void main(String[] args) {
		
		
		final UseManyCondition umc = new UseManyCondition();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				umc.m1();
			}
		},"t1");
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				umc.m2();
			}
		},"t2");
		Thread t3 = new Thread(new Runnable() {
			@Override
			public void run() {
				umc.m3();
			}
		},"t3");
		Thread t4 = new Thread(new Runnable() {
			@Override
			public void run() {
				umc.m4();
			}
		},"t4");
		Thread t5 = new Thread(new Runnable() {
			@Override
			public void run() {
				umc.m5();
			}
		},"t5");
		
		t1.start();	// c1
		t2.start();	// c1
		t3.start();	// c2
		

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		t4.start();	// c1
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		t5.start();	// c2
		
	}
	
	
	
}

运行结果:

结果分析: 线程1 2 4 对应一个Condition 线程4唤醒线程1和2 线程3 5 对应另一个Condition 线程5唤醒线程3

ReentrantReadWriteLock 读写锁

读写锁ReentrantReadWriteLock 核心是实现读写分离的锁 在高并发访问下 尤其是读多写少 性能远高于重入锁 本质是分成两个锁 读锁和写锁 在读锁下 多个线程可以并发的进行访问 但在写锁的时候 只能一个个的顺序访问 读读共享 写写互斥 读写互斥

上代码:

代码语言:javascript复制
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

public class UseReentrantReadWriteLock {

	private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
	private ReadLock readLock = rwLock.readLock();
	private WriteLock writeLock = rwLock.writeLock();
	
	public void read(){
		try {
			readLock.lock();
			System.out.println("当前线程:"   Thread.currentThread().getName()   "进入...");
			Thread.sleep(3000);
			System.out.println("当前线程:"   Thread.currentThread().getName()   "退出...");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			readLock.unlock();
		}
	}
	
	public void write(){
		try {
			writeLock.lock();
			System.out.println("当前线程:"   Thread.currentThread().getName()   "进入...");
			Thread.sleep(3000);
			System.out.println("当前线程:"   Thread.currentThread().getName()   "退出...");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			writeLock.unlock();
		}
	}
	
	public static void main(String[] args) {
		
		final UseReentrantReadWriteLock urrw = new UseReentrantReadWriteLock();
		
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				urrw.read();
			}
		}, "t1");
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				urrw.read();
			}
		}, "t2");
		Thread t3 = new Thread(new Runnable() {
			@Override
			public void run() {
				urrw.write();
			}
		}, "t3");
		Thread t4 = new Thread(new Runnable() {
			@Override
			public void run() {
				urrw.write();
			}
		}, "t4");		
		
		t1.start();
		t2.start();
		
//		t1.start(); // R 
//		t3.start(); // W
		
		t3.start();
		t4.start();
		
		
		
		
		
		
		
		
	}
}

运行结果:

线程3和4 是写操作 顺序执行 线程1 2 是读操作 同时进入和退出

0 人点赞