任何人都可以解释如何在 java 中使用重入锁而不是同步一些最佳示例



当我在 http://javarevisited.blogspot.in/2013/03/reentrantlock-example-in-java-synchronized-difference-vs-lock.html 运行示例类时,我看到的行为与synchronized相同。

这里有三种方法,方法,线程访问锁,一种松开锁。您可能想尝试使用 synchronized 关键字实现这些。使用ReentrantLock的扩展功能和优势将变得显而易见。

public class DoorLockUsingLock {
    private int counter= 0;
    private Thread owner= null;
    private Lock l = new ReentrantLock();
    private Condition notLocked= l.newCondition();
    public void lockItDown() throws InterruptedException {
        l.lockInterruptibly();
        try {
            while ((counter> 0) && (owner!= Thread.currentThread())) {
                notLocked.await();
            }
            counter++;
            owner = Thread.currentThread();
        } finally {
            l.unlock();
        }
    }
    public void lockItDownUninterruptibly() {
        l.lock();
        try {
            while ((counter > 0) && (owner != Thread.currentThread())) {
                notLocked.awaitUninterruptibly();
            }
            counter++;
            owner= Thread.currentThread();
        } finally {
            l.unlock();
        }
    }
    public boolean tryLockItDown(long timeout, TimeUnit unit) throws InterruptedException {
        long time = unit.toNanos(timeout);
        long end = System.nanoTime() + time;
        boolean success = l.tryLock(timeout, unit);
        if (!success) {
            return false;
        }
        try {
            time = end- System.nanoTime();
            while ((counter> 0) && (owner != Thread.currentThread()) && (time > 0)) {
                notLocked.await(time, TimeUnit.NANOSECONDS);
                time = end - System.nanoTime();
            }
            if (time > 0) {
                counter++;
                owner = Thread.currentThread();
                return true;
            }
            return false;
        } finally {
            l.unlock();
        }
    }
    public void unlockIt() throws IllegalMonitorStateException {
        l.lock();
        try {
            if (counter== 0) {
                throw new IllegalMonitorStateException();
            }
            if (owner!= Thread.currentThread()) {
                throw new IllegalMonitorStateException();
            }
            counter--;
            if (counter == 0) {
                owner = null;
                notLocked.signal();
            }
        } finally {
            l.unlock();
        }
    }
}

来自ReetrantLock类的JavaDoc:

具有相同基本行为和Lock 语义作为使用 synchronized 访问的隐式监视器锁 方法和语句,但具有扩展功能。

在您的示例中,您不使用"扩展功能";您使用 ReentrantLock 作为 synchronized 方法的等效替代方法(除了使用 synchronized 语句时,使用 this 作为锁(。因此,这两种方法的行为必须相同。

不,您通常不会看到行为上的区别。但正如网站所说,有几种情况您需要使用ReentrantLock而不是synchronized

  1. 您希望公平地选择等待线程。
  2. 你想使用 tryLock(( 方法。
  3. 您想中断等待线程并让它执行其他操作。
  4. ReentrantLock 的性能优于同步,您关心这一点。

如果您不需要任何这些改进,使用synchronized就可以了,并且您可能无法分辨出其中的区别。

最新更新