• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Thread yielding

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Quoted from RHE:

Note that most schedulters do not stop the yielding thread from running in favor of a thread of lower priority.


<font color=#7b007b>
I could not quite understand this due to my English. Isn't it true that the yielding thread waits for ANY Ready to Run threads to run?
Thanks,
Jenny
</font>
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
Actually, yield() is just an optimization suggestion to the thread scheduler.
Different implementors of a JVM are free to use this suggestion or not as they see fit. On some JVMs, yield() will do nothing. On others, it will actually pause the current thread and look for a different thread to run. It all depends on the specific algorithm they've implemented.
Basically, you can't make any definitive statements about how yield() works. In this regard, it is similar to the concept of calling System.gc(); There's no guarantee it will actually do anything when you call it!
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If yeild doesn't work calling sleep(0) usually does the trick to allow a thread context-switch to take place
[ March 12, 2002: Message edited by: Rajinder Yadav ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, but then you'll have to implement code to interrupt the thread from an external thread, otherwise it will sleep forever.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody !
I was searching for 'switch case construct' on the forum and I got 3 unrelated topics. This one I don't understand, my original doubt will follow .
1) The thread in my sample program is not interrupted and sleep(0) I believe may[not] lead to context switch depending on other runnable threads/priority etc. Though it will incurr the cost of a mode change. Is it right to say that the thread may sleep forever or a context switch WILL happen ?
My simple program prints "going to sleep" and "no sleep allowed."
2. In the BBrogden's 19 questions exam, the switch can take char/int/short only. I have tested with byte and it works. What is the right thing ?
thanks,
Ratna.
import java.util.*;
public class sleep extends Thread {
public static void main(String [] args ) {
sleep s = new sleep();
s.start();
}
public void run() {
System.out.println("going to sleep");
try {
sleep(0);
}
catch( InterruptedException e ) {
System.out.println("oh no !"); }
System.out.println("no sleep allowed ? ");
}
}
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Ross:
True, but then you'll have to implement code to interrupt the thread from an external thread, otherwise it will sleep forever.


??? ???
If you use Thread.sleep(0), that should make the thread sleep for 0 milliseconds before it is put back in to the ready to run state, right? I always thought this was a good way to make a thread give up the processor to allow other threads to do their work. I normally used this when a thread was waiting for something to happen before continuing. The thread would check quickly to see if it could continue and, if not, it would make a call to Thread.sleep(0) in order to give up the processor. Something like this:

Am I incorrect about the way I'm thinking about Thread.sleep()?
Thanks,
Corey
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ratna,
<QUOTE>2. In the BBrogden's 19 questions exam, the switch can take char/int/short only. I have tested with byte and it works. What is the right thing? </QUOTE>
I found similar question in BBrogden's exam in cram book with correct answer.
Question 11:
Which of these statements about the value that appears in a switch statement is correct?
a. The value can be of type char
b. The value can be of type byte
c. The value can be of type long
d. The value can be of type boolean
answer: a, b
Reasoning: a char or byte can be used, but only if all the case statement constants are in the correct range.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to this question, if it turns out that sleep(0) is not safe to use to fake a tread interruption, would sleep(0,1) do the trick?
Hey, 1 nanosecond is not much but its better than nothing!
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I was wrong about sleep(0). A thread does in fact, sleep for zero milliseconds. Well, it actually has to start sleeping, but then it will be eligible for scheduling immediately.
I'm POSITIVE I had read somewhere that calling sleep(0) was implemented as sleeing forever. But I just did some tests and that is not the case. So either the source was wrong, or the JVM has changed its implementation over time, or maybe I have a brain tumor. Not only does sleep(0) not sleep forever, but also I cannot find any references anywhere that support this claim.
So , forget what I said, I was wrong. sorry!
[ March 22, 2002: Message edited by: Rob Ross ]
 
Ratna Singh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rob, no problem. Thanks for the response.
Ratna
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Corey
The code using sleep(0) waste time in a loop waiting for a condition to be satisfied. Have you considered using wait and notify for the thread that makes the condition true notifies the waiting thread directly, instead of being "prevented" (at impredictable times) by the check of the sleeping thread?
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow I didn't think my short comment would generate so much discussion.
What sleep(0) does is allow the scheduler to perform a context switch if it's needed. The scheduler checks to see if there are any threads in the 'waiting' queue, if there isone, a context switch occurs and the current thread ends up being swapped out (that is, it yields to another thread).
You can't use wait/notify is some places, an example where yield() would be required is in a routine that is doing a cpu intensive task, if this thread is preventing another thread that updates (draws) to screen, or takes input from a user, then the screen will not get refreshed nor the input get registered until the task gets completed! So it might be a good idea to call yeild() once in a while to make your app look more responsive by allowing these other (most likely lower priority) threads to get some cpu cycles.
By the way, you should never be calling yield() or sleep(0) each time through a loop, that would waste too many cpu cycles, rather it's a good idea to call these functions every once in a while.
In java sleep(0) might not be the best way to do yielding (c/c++ win32 it's the only way). I would use yield() where ever possible, sleep(0) will most likely put a running thread into a ready state while yield() will keep a thread in the running state if no other threads needs the cpu. Going from a running state to a ready state to a running state is a needless waste of time!

Originally posted by Jose Botella:
Hello Corey
The code using sleep(0) waste time in a loop waiting for a condition to be satisfied. Have you considered using wait and notify for the thread that makes the condition true notifies the waiting thread directly, instead of being "prevented" (at impredictable times) by the check of the sleeping thread?

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic