使用Timeout避免Java多线程中的死锁

Kau*_*ele 11 java multithreading deadlock timeout locking

避免Java多线程中死锁情况的策略之一是使用超时.假设,一个线程已获得对一个资源的锁定,现在正在等待另一个资源上的锁定.经过一段时间后,如果无法获取资源2上的锁,那么它应该停止等待资源2的锁定.它也应该释放对resource1的锁定.因此将避免死锁.

但是如何在Java中实现它?如何明确"释放"锁定?如何定义等待锁定的超时.

什么是精确的java命令和语法.请问任何问候世界的例子吗?

ass*_*ias 10

这是一个人为的例子,有2个锁和2个线程,试图以不同的顺序获取它们.没有超时,代码就会死锁.

public static void main(String[] args) throws Exception {
    final ReentrantLock lock1 = new ReentrantLock();
    final ReentrantLock lock2 = new ReentrantLock();
    Runnable try1_2 = getRunnable(lock1, "lock 1", lock2, "lock 2");
    Runnable try2_1 = getRunnable(lock2, "lock 2", lock1, "lock 1");
    new Thread(try1_2).start();
    new Thread(try2_1).start();
}

private static Runnable getRunnable(final ReentrantLock lock1, final String lock1Name, final ReentrantLock lock2, final String lock2Name) {
    return new Runnable() {
        @Override
        public void run() {
            try {
                if (lock1.tryLock(1, TimeUnit.SECONDS)) {
                    System.out.println(lock1Name + " acquired in thread " + Thread.currentThread());
                    if (lock2.tryLock(1, TimeUnit.SECONDS)) {
                        System.out.println(lock2Name + " acquired in thread " + Thread.currentThread());
                        Thread.sleep(2000);
                    } else {
                        System.out.println("Could not acquire "+lock2Name + " in thread " + Thread.currentThread());
                        lock1.unlock();
                        System.out.println(lock1Name + " released in thread " + Thread.currentThread());
                    }
                } else {
                    System.out.println("Could not acquire " + lock1Name + " in thread " + Thread.currentThread());
                }
            } catch (InterruptedException e) {
                //you should not ignore it
            } finally {
                if (lock1.isHeldByCurrentThread()) lock1.unlock();
                if (lock2.isHeldByCurrentThread()) lock2.unlock();
            }
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

  • @assylias当调用对象的同步方法时,线程会自动尝试获取“该对象上的锁”。然而,在使用“java.util.concurrent.locks.Lock”时,我们不会请求“锁定该对象的对象”,我们正在调用其方法。所以我认为,为了正确使用“Lock”类,我们不应该将方法标记为同步。但我们应该有一个与该对象关联的“Lock”实例。在调用需要同步的代码之前,我们应该在相关锁上使用 tryLock() 。---- 请继续阅读下面的评论 ------ (2认同)