尝试在子线程中获取父线程持有锁

本文通过ReentrantLock与synchronized关键字的实际测试,验证了子线程是否可以获取到已被父线程持有的锁。结果显示,在两种情况下,子线程都能成功获取锁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前有面试官问过是否可以在子线程中获取父线程所持有的锁,我根据以前看过的 ReentrantLock 的源码分析, AQS 的队首节点中保存的是成功获取同步状态 state 的线程引用,与父子线程无关,所以是不可以的

今天来实际的测试一下


1. ReentrantLock 测试

/**
 * Author:  heatdeath
 * Date:    2018/7/15
 * Desc:
 */
public class GetLockInChildThreadDemo {
    private static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        lock.lock();
        try {
            System.out.println(Thread.currentThread() + " i got the lock");
            new Thread(() -> {
                lock.lock();
                try {
                    System.out.println(Thread.currentThread() + " I got the lock and going to sleep");
                    Thread.sleep(5000);
                    System.out.println(Thread.currentThread() + " I am waked up");
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                } finally {
                    lock.unlock();
                }
            }).start();
            Thread.sleep(6000);
            System.out.println(Thread.currentThread() + " I am waked up");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            lock.unlock();
        }
    }

}

输出

Thread[main,5,main] i got the lock
Thread[main,5,main] I am waked up
Thread[Thread-0,5,main] I got the lock and going to sleep
Thread[Thread-0,5,main] I am waked up

这里写图片描述


2. synchronized 测试

/**
 * Author:  heatdeath
 * Date:    2018/7/15
 * Desc:
 */
public class GetLockInChildThreadDemo {

    public static void main(String[] args) {

        synchronized (GetLockInChildThreadDemo.class) {
            try {
                System.out.println(Thread.currentThread() + " i got the lock");

                new Thread(() -> {
                    synchronized (GetLockInChildThreadDemo.class) {
                        try {
                            System.out.println(Thread.currentThread() + " I got the lock and going to sleep");
                            Thread.sleep(5000);
                            System.out.println(Thread.currentThread() + " I am waked up");
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                    }
                }).start();

                Thread.sleep(6000);
                System.out.println(Thread.currentThread() + " I am waked up");
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

    }
}

输出:

Thread[main,5,main] i got the lock
Thread[main,5,main] I am waked up
Thread[Thread-0,5,main] I got the lock and going to sleep
Thread[Thread-0,5,main] I am waked up

GetLockInChildThreadDemo.class 对象只有一个 moniter ,只有等待 main 线程执行到 moniterexit 指令时,子线程才能执行 moniterenter 获取锁

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值