Skip to content

05_可重入锁

1. 什么是可重入锁

可重入锁是一种特殊的锁,它允许一个线程多次获取同一把锁。可重入锁的主要作用是避免死锁,提高代码的可靠性和可维护性。

2. synchronized 是可重入锁, ReentrantLock 是可重入锁

在 Java 中,synchronized 是一种可重入锁,它允许一个线程多次获取同一把锁。ReentrantLock 也是一种可重入锁,它提供了更多的功能和灵活性,如可定时、可中断、可公平等。

3. 可重入锁的使用

以下是一个使用synchronized可重入锁的示例:

java
public class SynchronizedReentrantExample {
    public synchronized void doWork() {
        System.out.println("Lock acquired by thread: " + Thread.currentThread().getName());
        doSubWork();
    }
    public synchronized void doSubWork() {
        System.out.println("Lock acquired by thread: " + Thread.currentThread().getName());
    }
    public static void main(String[] args) {
        SynchronizedReentrantExample example = new SynchronizedReentrantExample();
        Thread t1 = new Thread(() -> example.doWork());
        Thread t2 = new Thread(() -> example.doWork());
        t1.start();
        t2.start();
    }
}

以下是一个使用ReentrantLock可重入锁的示例:

java
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockExample {
    private ReentrantLock lock = new ReentrantLock();
    public void doWork() {
        lock.lock();
        try {
            System.out.println("Lock acquired by thread: " + Thread.currentThread().getName());
            doSubWork();
        } finally {
            lock.unlock();
        }
    }
    public void doSubWork() {
        lock.lock();
        try {
            System.out.println("Lock acquired by thread: " + Thread.currentThread().getName());
        } finally {
            lock.unlock();
        }
    }
    public static void main(String[] args) {
        ReentrantLockExample example = new ReentrantLockExample();
        Thread t1 = new Thread(() -> example.doWork());
        Thread t2 = new Thread(() -> example.doWork());
        t1.start();
        t2.start();
    }
}

4. 不可重入锁

不可重入锁是一种特殊的锁,它不允许一个线程多次获取同一把锁。不可重入锁的主要作用是避免死锁,提高代码的可靠性和可维护性。 java中没有内置的不可重入锁,但可以通过自定义实现来实现不可重入锁。 以下是一个简单的不可重入锁的示例:

java
public class NonReentrantLock {
    // 锁标志
    private boolean isLocked = false;
    // 加锁
    public synchronized void lock() throws InterruptedException {
        while (isLocked) {
            // 如果已经加锁了,等待锁释放
            wait();
        }
        isLocked = true;
    }
    public synchronized void unlock() {
        // 释放锁
        isLocked = false;
        notify();
    }
    public static void main(String[] args) {
        NonReentrantLock lock = new NonReentrantLock();
        Thread t1 = new Thread(() -> {
            try {
                lock.lock();
                System.out.println("Lock acquired by thread: " + Thread.currentThread().getName());
                lock.lock();
                System.out.println("Lock acquired by thread: " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                lock.unlock();
            }
        });
        Thread t2 = new Thread(() -> {
            try {
                lock.lock();
                System.out.println("Lock acquired by thread: " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        });
        t1.start();
        t2.start();
    }
}