当我在http://javarevisited.blogspot.in/2013/03/reentrantlock-example-in-java-synchronized-difference-vs-lock.html运行示例类时,我看到与synchronized
相同的行为。
答案 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
。
如果您不需要任何这些改进,使用synchronized
就可以了,您可能无法区分它们。