In Java if a thread, t2
, attempts to attain a lock, from synchronized, which is currently in use by another thread, t1
, then t2
will switch from runnable to blocked. Correct? What about with ReentrantLock
s?
If the thread t1
finishes using the lock, does t2
then automatically switch back to runnable or do you need to use notifyAll()
? What about with ReentrantLock
usage without a condition. If you aren't using a condition how do you inform the thread t2
that it should switch back to runnable? Is it ever wise, or even possible to use reentrant locks without a condition?
If this question has already been answered (I couldn't find it), I would be grateful if you would link it to me.
It sounds like you're confusing the blocked and waiting states. Blocked means that the thread is trying to acquire the lock and can't so is stuck. Waiting means the thread is dormant; it's hanging out until it receives a notification, or until it otherwise comes back from waiting (timeout, if called with a timeout value, or spurious wakeup).
Once a lock becomes available the OS scheduler has to decide which blocked thread gets it. The thread it picks to get the lock becomes runnable.
So notify pertains to waiting threads, not blocked ones. A thread that has the lock but which has figured out it can't progress (it detects the condition it's waiting for isn't true) can call wait on that lock, releasing the lock and going dormant. You use notify to tell the scheduler to wake up any one thread that is waiting on the lock. Once the thread is woken up it has to reacquire the lock it previously released before it can exit the wait method.
The basic behavior of ReentrantLock is analogous to intrinsic locks, except that you can have multiple conditions with reentrant locks. Keep in mind ReentrantLock has its own separate methods to call (await and signal instead of wait and notify). You would use conditions with ReentrantLock when you want the threads to wait and get notified, with different conditions used so that threads will be waiting only on conditions relevant to them.