任何人都可以解释如何在Java中使用Reentrant Lock与一些最佳示例同步

时间:2013-08-28 18:22:27

标签: java multithreading concurrency reentrantlock

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

3 个答案:

答案 0 :(得分:6)

以下是访问锁的线程的三种方法,一种用于释放锁的方法。您可能希望尝试使用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();
        }
    }
}

答案 1 :(得分:2)

来自the JavaDoc of the ReetrantLock class

  

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

在您的示例中,您不使用“扩展功能”;您使用ReentrantLock作为synchronized方法的等效替代方法(使用synchronized语句除外,您使用this作为锁定)。所以这两种方法的行为必须相同。

答案 2 :(得分:0)

不,你通常不会看到行为上的差异。但正如网站所说,有几种情况你想要使用ReentrantLock代替synchronized

  1. 您希望等待线程得到公平选择。
  2. 您想使用tryLock()方法。
  3. 你想打断一个等待的线程并让它做其他事情。
  4. ReentrantLock的性能优于同步,你关心的是。
  5. 如果您不需要任何这些改进,使用synchronized就可以了,您可能无法区分它们。

相关问题