ReentrantLock的响应中断

本文深入探讨了ReentrantLock的特性,包括可中断、可轮询和定时锁的功能,以及如何通过Interruptibly方法避免死锁。通过示例代码展示了如何使用两把锁在多线程环境下进行资源访问,同时提供了自旋检查和主动中断机制来预防死锁的发生。

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

/**
 * ReentrantLock锁是可中断的,可轮询的,定时锁
 * lock1.lockInterruptibly()上锁
 * tryLock方法可以加参数,实现定时锁
 * ReentrantLock可以设置公平锁和非公平锁,在构造函数中加boolean fail参数,默认是非公平锁
 */
public class InterruptiblyLock {
    public ReentrantLock lock1=new ReentrantLock();//第一把锁1
    public ReentrantLock lock2=new ReentrantLock();//第二把锁lock2
    public Thread lock1(){
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock1.lockInterruptibly();//如果当前线程未被中断,则获取锁
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock2.lockInterruptibly();
                    System.out.println(Thread.currentThread().getName()+"执行完毕");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    if (lock1.isHeldByCurrentThread()){
                        lock1.unlock();
                    }
                    if (lock2.isHeldByCurrentThread()){
                        lock2.unlock();
                    }
                    System.out.println(Thread.currentThread().getName()+"退出");
                }
            }
        });
        t.start();
        return t;
    }

    public Thread lock2(){
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock2.lockInterruptibly();
                    try{
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock1.lockInterruptibly();
                    System.out.println(Thread.currentThread().getName()+"执行完毕");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    if (lock1.isHeldByCurrentThread()){
                        lock1.unlock();
                    }
                    if (lock2.isHeldByCurrentThread()){
                        lock2.unlock();
                    }
                    System.out.println(Thread.currentThread().getName()+"退出");
                }
            }
        });
        t.start();
        return t;
    }

    public static void main(String[] args) throws InterruptedException{
        long time=System.currentTimeMillis();
        InterruptiblyLock interruptiblyLock=new InterruptiblyLock();
        Thread thread1=interruptiblyLock.lock1();
        Thread thread2=interruptiblyLock.lock2();
        //自旋一段时间,如果等待时间过长,则可能发生了死锁等待问题,主动中断并释放锁。
        while (true){
            if (System.currentTimeMillis()-time>=300){
                //线程2退出,让出锁,1拿到锁执行。
                thread2.interrupt();//中断线程1
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值