This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
As we know calling sleep from any thread halts the execution of the given thread for a specific period of time without losing the monitor of the thread. On the other hand invoking Thread.yield() causes the currently executing thread object to temporarily pause and allow other threads to execute.
Now my doubt is
1. Whehther calling Thread.yield() does not lose the monitor of the thread like the sleep() method.
2. Does sleep(0) behave like Thread.yield(). That is does it return immediately like Thread.yield() after it has allowed other threads to execute.
Well regarding the second point i doubt that it works that way because as far as i know sleep method forces the thread to halt execution for a specific period of time during which other threads are allowed to execute.
What i want is this that without losing any monitor on any object that a thread holds i want that thread to relinquish control of the cpu so that other threads can execute. However if no threads are there to be executed then the control sould immediately return without any dealy.
In a nutshell i want a Thread.yield() behavior without losing any sort of object monitors that the concerned thread might be holding.