可重入性描述这样的一个问题:一个线程在持有一个锁的时候,它内部能否再次(多次)申请该锁。如果一个线程已经获得了锁,其内部还可以多次申请该锁成功。那么我们就称该锁为可重入锁。通过以下伪代码说明:
void methodA(){ lock.lock(); // 获取锁 methodB(); lock.unlock() // 释放锁 } void methodB(){ lock.lock(); // 获取锁 // 其他业务 lock.unlock();// 释放锁 }
public class ReentrantLockDemo01 implements Runnable { private Lock lock = new ReentrantLock(); private int tickets = 200; @Override public void run() { while (true) { lock.lock(); // 获取锁 try { if (tickets > 0) { TimeUnit.MILLISECONDS.sleep(100); System.out.println(Thread.currentThread().getName() + " " + tickets--); } else { break; } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); // 释放所 } } } public static void main(String[] args) { ReentrantLockDemo01 reentrantLockDemo = new ReentrantLockDemo01(); for (int i = 0; i < 10; i++) { Thread thread = new Thread(reentrantLockDemo, "thread" + i); thread.start(); } } }
从Lock的源码可以看出:lockInterruptibly() 抛出中断异常
void lockInterruptibly() throws InterruptedException;
在synchronize关键字中,同步代码块发送阻塞的情况,例如:wait(),sleep(),jion()等情况下,可以被中断。中断并不意味着线程已经终止
代码示例如下:
public class ReentrantLockDemo02 implements Runnable { private Lock lock = new ReentrantLock(); @Override public void run() { try { lock.lockInterruptibly(); System.out.println(Thread.currentThread().getName() + " running"); Thread.sleep(2000); System.out.println(Thread.currentThread().getName() + " finished"); lock.unlock(); } catch (InterruptedException e) { System.out.println(Thread.currentThread().getName() + " interrupted"); } } public static void main(String[] args) { ReentrantLockDemo02 reentrantLockDemo = new ReentrantLockDemo02(); Thread thread01 = new Thread(reentrantLockDemo, "thread01"); Thread thread02 = new Thread(reentrantLockDemo, "thread02"); thread01.start(); thread02.start(); thread02.interrupt(); } }
输出结果:
thread01 running
thread02 interrupted
thread01 finished
public class ReentrantLockDemo03 implements Runnable { private ReentrantLock lock = new ReentrantLock(); @Override public void run() { try { if (lock.tryLock(2, TimeUnit.SECONDS)) { System.out.println(Thread.currentThread().getName() + " 获取当前lock锁"); TimeUnit.SECONDS.sleep(4); } else { System.out.println(Thread.currentThread().getName()+ " 获取锁失败"); } } catch (InterruptedException e) { e.printStackTrace(); } finally { if (lock.isHeldByCurrentThread()) { lock.unlock(); } } } public static void main(String[] args) { ReentrantLockDemo03 reentrantLockDemo = new ReentrantLockDemo03(); Thread thread01 = new Thread(reentrantLockDemo, "thread01"); Thread thread02 = new Thread(reentrantLockDemo, "thread02"); thread01.start(); thread02.start(); } }
public class ProducerConsumerTest { private Lock lock = new ReentrantLock(); private Condition addCondition = lock.newCondition(); private Condition removeCondition = lock.newCondition(); private LinkedList<Integer> resources = new LinkedList<>(); private int maxSize; public ProducerConsumerTest(int maxSize) { this.maxSize = maxSize; } public class Producer implements Runnable { private int proSize; private Producer(int proSize) { this.proSize = proSize; } @Override public void run() { lock.lock(); try { for (int i = 1; i < proSize; i++) { while (resources.size() >= maxSize) { System.out.println("当前仓库已满,等待消费..."); try { addCondition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("已经生产产品数: " + i + "\t现仓储量总量:" + resources.size()); resources.add(i); removeCondition.signal(); } } finally { lock.unlock(); } } } public class Consumer implements Runnable { @Override public void run() { String threadName = Thread.currentThread().getName(); while (true) { lock.lock(); try { while (resources.size() <= 0) { System.out.println(threadName + " 当前仓库没有产品,请稍等..."); try { // 进入阻塞状态 removeCondition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } // 消费数据 int size = resources.size(); for (int i = 0; i < size; i++) { Integer remove = resources.remove(); System.out.println(threadName + " 当前消费产品编号为:" + remove); } // 唤醒生产者 addCondition.signal(); } finally { lock.unlock(); } } } } public static void main(String[] args) throws InterruptedException { ProducerConsumerTest producerConsumerTest = new ProducerConsumerTest(10); Producer producer = producerConsumerTest.new Producer(100); Consumer consumer = producerConsumerTest.new Consumer(); final Thread producerThread = new Thread(producer, "producer"); final Thread consumerThread = new Thread(consumer, "consumer"); producerThread.start(); TimeUnit.SECONDS.sleep(2); consumerThread.start(); } }
我这儿整理了比较全面的JAVA相关的面试资料,

需要领取面试资料的同学,请加群:473984645

原文:https://www.cnblogs.com/1013wang/p/11820373.html