Home arrow static arrow Java Programming [Archive] - Thread.yield()!!
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Thread.yield()!!
This topic has 8 replies on 1 page.

Posts:40
Registered: 7/15/01
Thread.yield()!!  
Sep 4, 2001 9:10 AM



 

Hi all,
just felt inquisitive about "Thread.yield()"..
In the api specification it is only mentioned that
the thread gives up control for other thread to execute..
but there is no mention about when the thread will
get back the control!!. Like in Thread.sleep(nnn)
it is mentioned that after some time period the thread
will start execution again..what about Thread.yield?
Can anybody highlight..
thanks.
 

Posts:642
Registered: 6/11/97
Re: Thread.yield()!!  
Sep 4, 2001 9:21 AM (reply 1 of 8)



 
The answer is "The next time the thread is eligible for execution." In other words, you don't know. It depends on:

1 - The priority level of the yielding thread.
2 - How many threads are running at the same priority, and how active they are.
3 - How many threads are running at a higher priority, and how active they are.
4 - How utilized your CPU is in general.

The timing of when a thread wakes from yield is similar to when a thread will regain control after implicitly being pre-empted, which is also undefined.

I actually never call yield - I try to design my threads so that they can use explicit sleeps like Thread.sleep() and Object.wait(). There is an article about it in the new Effective Java book published by JavaSoft:

http://www.amazon.com/exec/obidos/ASIN/0201310058/qid=999620161/sr=2-1/103-9251192-1660633
 

Posts:18
Registered: 5/28/04
Re: Thread.yield()!!  
Aug 5, 2004 8:37 PM (reply 2 of 8)



 
Thread.yield() is used in the case of an infinity loop. In the loop, the current thread hold an object monitor:
while(true)
{
synchronized(o)
{
//................;
//................;
Thread.yield();
}
}
Think about without Thread.yield(), other threads would have less oppotunity to gain control of the monitor o, that's the use of Thread.yield(), very different from sleep(mmm).
 

Posts:5,965
Registered: 5/17/03
Re: Thread.yield()!!  
Aug 5, 2004 11:21 PM (reply 3 of 8)



 
The runtime system lets each thread run for a little while. If a thread does very little it can voluntarily give up the reminder of its time slice using yield() instead of just sitting it out.
 

Posts:18,384
Registered: 21.03.00
Re: Thread.yield()!!  
Aug 5, 2004 11:46 PM (reply 4 of 8)



 
Hi,

It's also dependent on the process scheduling algorithm of the OS (or thread library implementation). We who were using JDK 1.0.2 on unix had to call yield since the threads with same priority wasn't executed in round robin.

/Kaj
 

Posts:161
Registered: 8/7/97
Re: Thread.yield()!!  
Aug 6, 2004 3:39 AM (reply 5 of 8)



 
Thread.yield() is used in the case of an infinity
loop. In the loop, the current thread hold an object
monitor:
while(true)
{
synchronized(o)
{
//................;
//................;
Thread.yield();
}
}
Think about without Thread.yield(), other threads
would have less oppotunity to gain control of the
monitor o, that's the use of Thread.yield(), very
different from sleep(mmm).

This seems not correct. Yield just allows other threads to run, but it doesn't release any monitors. You don't realeas monitors when the OS selects an different thread to be executed and Thread.yield does "nothing else" than the scheduler.. Just imagine what would happen if you could call Thread.yield() in some callback code that is called by a library that uses synchronization? Do you really thinkg it makes sense that all monitors are realeased then?
 

Posts:6,487
Registered: 5/5/04
Re: Thread.yield()!!  
Aug 6, 2004 4:41 AM (reply 6 of 8)



 
Using the yield() in an infinite loop is back practice as this loop will always use all free CPU.
Other threads should behave correctly but it is still a bad idea if you want the machine to run anything else.

In stead you should use wait() This will wait until another thread calls notify or the timeout is reached.
This will limit the CPU consumption.
synchronized(o) {    while(true) {       if (o.needToWait())             try {                  o.wait(TIME_OUT);                  continue;              } catch(InterruptedException ie) {                   ie.printStackTrace();                   continue;              }       // some code.     }}  // somewhere elsesynchronized(o) {     // update o     o.notifyAll();}
 

Posts:426
Registered: 11/1/00
Re: Thread.yield()!!  
Aug 6, 2004 3:57 PM (reply 7 of 8)



 
As stated earlier, Thread.yield() may not be implemented in many JVMs/OSs (those 1:1 thread models) as they are preempted by OS action and kernel scheduling anyway, many implement Thread.sleep() instead to actually have the JVM usleep()/nanosleep().
 

Posts:826
Registered: 7/1/03
Re: Thread.yield()!!  
Aug 6, 2004 4:07 PM (reply 8 of 8)



 
Joshua Bloch put up the definite argument against using yield() for me. (Essential Java)
I'm still dealing with the consequences in some progs, but wow did it make a difference when I stopped using yield() as a crutch for inadequate concurrency (I mean like 600% throughput increase).

/k1
 
This topic has 8 replies on 1 page.