what the purpose of Thread.holdsLock(lock)?
java, multithreading
Solution
The javadoc of the method says:
Returns `true` if and only if the current thread holds the monitor lock on the specified object.
(emphasis mine)
So, the assert checks that the current thread does not hold the monitor lock of the given lock object.
Note that asserts are used to check invariants, and can be disabled. They should not be used to prevent deadlock. A regular `if` test should be used to do that.
Problem
I saw someone to use `assert !Thread.holdsLock(lock)` to avoid deadlock. what's the purpose of this? If the lock object is held by another thread, will assert cause the code to exit immediately?