1.ReentrantLock 简单的使用
private Lock lock = new ReentrantLock();
lock.lock();用来获取锁。
lock.unlock();用来释放锁
package com.qey.lock;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;/*** @ClassName UseReentrantLock* @Description volte* @Author * @Date 2020-05-13 15:03* @Version 1.1**/
public class UseReentrantLock {private Lock lock = new ReentrantLock();public void method1(){try {lock.lock();System.out.println("当前线程 method1"+Thread.currentThread().getName()+"进入");Thread.sleep(1000);System.out.println("当前线程 method1"+Thread.currentThread().getName()+"退出");Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}finally {lock.unlock();}}public void method2(){try {lock.lock();System.out.println("当前线程 method2"+Thread.currentThread().getName()+"进入");Thread.sleep(1000);System.out.println("当前线程 method2"+Thread.currentThread().getName()+"退出");Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}finally {lock.unlock();}}static class RunnableDemo1 implements Runnable{private UseReentrantLock useReentrantLock;public RunnableDemo1(UseReentrantLock useReentrantLock) {this.useReentrantLock = useReentrantLock;}@Overridepublic void run() {useReentrantLock.method1();}}static class RunnableDemo2 implements Runnable{private UseReentrantLock useReentrantLock;public RunnableDemo2(UseReentrantLock useReentrantLock) {this.useReentrantLock = useReentrantLock;}@Overridepublic void run() {useReentrantLock.method2();}}public static void main(String[] args) {//这里是测试,尽量使用ThreadPoolExecutor 创建线程,方式资源泄漏UseReentrantLock useReentrantLock = new UseReentrantLock();ExecutorService executorService = Executors.newFixedThreadPool(3);executorService.execute(new RunnableDemo1(useReentrantLock));executorService.execute(new RunnableDemo2(useReentrantLock));}
}
2.使用 ReentrantLock 实现生产者和消费者
package com.qey.lock;import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;/*** @ClassName ConditionBuffer* @Description volte* @Author* @Date 2020-05-13 15:44* @Version 1.1**/
public class ConditionBuffer implements Buffer {private Lock lock = new ReentrantLock();private Condition put = lock.newCondition();private Condition take = lock.newCondition();private final static int CAPACITY = 1;private ArrayList<Integer> arrayList = new ArrayList<>(CAPACITY);private int count = -1;public ArrayList<Integer> getArrayList() {return arrayList;}@Overridepublic void put(Integer integer) throws InterruptedException {if (integer == null) {return;}lock.lock();try {while (count == CAPACITY) {put.await();System.out.println("ReentantLock put "+ Thread.currentThread().getName());}arrayList.add(integer);count++;take.signal();} catch (InterruptedException e) {e.printStackTrace();}finally {lock.unlock();}}@Overridepublic Integer take() throws InterruptedException {lock.lock();while (count == 0) {take.await();System.out.println("ReentantLock take "+ Thread.currentThread().getName());}try {Integer o = arrayList.get(count % CAPACITY);count--;put.signal();return o;} finally {lock.unlock();}}static class RunnableTake implements Runnable{private Buffer buffer;public RunnableTake(Buffer buffer) {this.buffer = buffer;}@Overridepublic void run() {try {while (true){buffer.take();}} catch (InterruptedException e) {e.printStackTrace();}}}static class RunnablePut implements Runnable{private Buffer buffer;public RunnablePut(Buffer buffer) {this.buffer = buffer;}@Overridepublic void run() {try {while (true){buffer.put(1);}} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args) {ConditionBuffer conditionBuffer = new ConditionBuffer();Thread thread1 = new Thread(new RunnablePut(conditionBuffer));thread1.start();Thread thread2 = new Thread(new RunnableTake(conditionBuffer));thread2.start();}
}
后续补充:ReentrantReadWriteLock 多线程并发之读写锁(ReentranReadWriteLock&ReadWriteLock)使用详解_小小默:进无止境-CSDN博客
参考:Java ReentrantLock中condition通信的好处 - 简书
JAVA中的ReentrantLock和ReentrantReadWriteLock锁简单使用_madman-CSDN博客_reentrantreadwritelock使用