I am trying to figure out how can we correctly wait until a lock is released in another thread.
I think the code will explain better what do I mean:
myLock.lock();
sendSomewhereMyLock(myLock); //creates new threads inside
myLock.waitUntilReleasedByAnotherThread(60L, TimeUnit.SECONDS);
//do something after the lock is released
I thought that tryLock(long time, TimeUnit unit)
is the method which I need, but Java docs says that the method will be executed immediately because the lock was acquired by current thread:
If the lock is available this method returns immediately with the value true. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:
- The lock is acquired by the current thread; or
- Some other thread interrupts the current thread, and interruption of lock acquisition is supported; or
- The specified waiting time elapses
What should I use then?