Regarding multiple locking attempts using java.concurrent.ReentrantLock
I noticed that the following code:
final Lock s = new ReentrantLock();
for(int i = 0 ; i < 1000 ; i++)
{
s.lock();
System.out.println(i+" :" +s.tryLock()+" ");
}
Prints:
0 :true
1 :true
2 :true
3 :true
...
It's odd - I would expect subsequent locks to fail since s is never unlocked.
Any errors here?
source to share
Javadoc is your friend . You should really read it.
From: ReentrantLock.lock ()
If the current thread already holds a lock, then the hold counter is incremented and the method returns immediately.
source to share
I bet you lock it over and over again with the same thread. In this case, the thread already owns the lock, so the lock is successfully acquired (since it doesn't even need to be acquired).
A ReentrantLock belongs to the last successful blocking of the thread, but has not yet unlocked it. A thread call lock returns by successfully acquiring the lock when the lock is not owned by another thread. The method will return immediately if the current thread already owns the lock. This can be checked using the isHeldByCurrentThread () and getHoldCount () methods.
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html
source to share