Mixing synchronized() with ReentrantLock.lock()

Posted by yarvin on Stack Overflow See other posts from Stack Overflow or by yarvin
Published on 2010-05-24T23:38:04Z Indexed on 2010/05/24 23:41 UTC
Read the original article Hit count: 153

Filed under:
|
|
|

In Java, do ReentrantLock.lock() and ReetrantLock.unlock() use the same locking mechanism as synchronized()?

My guess is "No," but I'm hoping to be wrong.

Example:

Imagine that Thread 1 and Thread 2 both have access to:

ReentrantLock lock = new ReentrantLock();

Thread 1 runs:

synchronized (lock) {
    // blah
}

Thread 2 runs:

lock.lock();
try {
    // blah
}
finally {
    lock.unlock();
}

Assume Thread 1 reaches its part first, then Thread 2 before Thread 1 is finished: will Thread 2 wait for Thread 1 to leave the synchronized() block, or will it go ahead and run?

© Stack Overflow or respective owner

Related posts about java

Related posts about locking