Tuesday, August 21, 2007

Some notes on the new java 1.5 concurrent api

In the new java 1.5 Concurrent api, there is a lock.tryLock method, which returns false if the lock is already acquired by some other thread.

I have been in situations where I needed to know if an object lock(via synchronized(object a)) had already been acquired or not.

I guess it is simple now with the above 1.5 api.

However the other important thing to know is, if an object lock is in wait state or not before calling wait or notify on the lock. The 1.5 api has a Condition interface, but i did not see any kind of isWaiting method.

In java 1.4 i have achieved this by using volatile booleans, setting the boolean just before calling wait and then unsetting the boolean just after the notify.

Since wait and notify need to happen in synchronized blocks, this method has worked. However i would like to have an isWaiting api for locks.


Post a Comment

<< Home