I am writing an API that is required to be thread-safe. As I know if a share object is enclosed by a sychronized block, any attempt to lock the object that is already locked will be blocked. My questions are i.) is it using thread.wait() to cause those threads giving up the CPU resource? ii.) If using reentrantreadWriteLock().writeLock() instead of synchronized block, how to let those blocked threads giving up CPU time?
The question is not very clear, but maybe the following will help.
If a thread is blocked waiting to gain the lock on an object's monitor (i.e. it is trying to enter a synchronized block), that thread uses hardly any CPU. Similarly, if a thread is in wait(), waiting for a notify() from another thread, the waiting thread uses hardly any CPU.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joined: Mar 28, 2007
I've checked with some books in which specify that a thread gives up the CPU resource and the lock on a shared object if it encountered a wait() call in the object. So the question left is how can I use reentrantReadWriteLock().writeLock() to do the same thing as done by sychronized block and wait() call? Once again, many thanks for your reply.