Thread blocked

Hi all,

I am writing an API 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?

Thanks in advance

[451 byte] By [cheetaha] at [2007-11-27 11:31:03]
# 1

typo - should be shared object.wait() .

> Hi all,

>

> I am writing an API 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?

>

>

> Thanks in advance

cheetaha at 2007-7-29 16:37:28 > top of Java-index,Core,Core APIs...
# 2

Threads that are waiting <Object.wait()> or sleeping <Thread.sleep()> are NOT taking any CPU time. I don't know the internals of the concurrency API, but I assume it uses Object.wait() since Thread.sleep does not release any monitor hold by the thread.

DikkeDouwea at 2007-7-29 16:37:28 > top of Java-index,Core,Core APIs...
# 3

Thanks Dikke,

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.

Big Cat

cheetaha at 2007-7-29 16:37:28 > top of Java-index,Core,Core APIs...
# 4

Hi all,

Please bear me so clumsy to post the same topic several times. Let me extend the question : Assume that a shared object is held by a writer with reentrantReadWriteLock().writeLock() call, while it is updating within the writeLock() block, a reader comes and tries to place a read-lock on the object. Obviously, the reader is blocked. In such case, does the reader still consume CPU time? If yes, how to let it release CPU resource (going to wait state).

Many thanks

cheetaha at 2007-7-29 16:37:28 > top of Java-index,Core,Core APIs...
# 5

The anser on your question is NO. Things that are blocking do not consume CPU.

DikkeDouwea at 2007-7-29 16:37:28 > top of Java-index,Core,Core APIs...