i am bit confused about need of yield method .Yield method Causes the currently executing thread object to temporarily pause and allow other threads(of higher priority or same priority) to execute.
At one of the article given at http://oreilly.com/catalog/expjava/excerpt/index.html it is given at If, at any time, a thread of a higher priority than the current thread becomes runnable, it preempts the lower priority thread and begins executing which is what yield is also doing/
So looks like from the above statements yield is automatically taken care by jvm.Sure i am missing something here. Looking for that miss?
Another question let say if we have two thread of different priority in runnablestate does jvm give the equal opportunity to both threads to execute or high priority thread will be completed first?
yield() is of questionable value. Different implementations treat it differently, some even ignore it.
The idea is that if you have a long running or compute intensive application it's always a good idea to play nice with others. Do some computing, give others a shot at the processor. I use a daemon to check for problems. It looks here, does a yield(). It looks there, does a yield() etc. The daemon may interfere with normal processing if it takes too long, so I try to be considerate of others. Does it work? Don't know. No one ever said it hurt.
Same implementation question with priority. I set the daemon at lowest, leave the others to default. There is no difference between a priority of 4, 5, 6, at least with Java. It all comes down to how the underlying operating systems treats priority.
I've used yield() on couple of occasions, usually with embedded devices and real-time environments.
Usually, the case is that I have some negative feedback loop: a loop that constantly checks the necessary sensors, and then responds to the sensor data in some way if the values are too far away from the desired value.
Meanwhile, if you have a second thread performing a different, but equally important task, it may not be responsive enough because it never gets a chance to run.
You can put yield() at the bottom of your busy loop to tell the runtime system that this would be a good moment to give other threads a chance to run.
Like Edward mentioned, it's not very reliable, but it doesn't hurt either. However, if you use yield(), you should always consider it as a hint to yourself that there may be a better way to provide responsiveness.
Here's a concrete example: Let's say my Java program is responsible for balancing a remote control toy car on top of a see-saw. The car has an accelerometer, which you can use to determine the angle of the see-saw. However, the only way to get data from this sensor is to poll it. It won't notify you if new data becomes available.
So we have a busy loop that constantly polls the accelerometer, uses the data to determine the angle of the see-saw, and then moves the car forward or backward in an attempt to balance the see-saw.
Now, we have a second thread that simply waits until it gets notified of radio messages, in case we want to manually override the car's controls. Radio messages may arrive, but the thread may not get a chance to respond to them if the negative-feedback loop is running. It appears that the car is not responding to our remote control, or very sluggish.
If we put yield() at the bottom of the feedback loop, it may give the radio thread a chance to run after it's woken up by a radio message.
I am having a doubt on the yield() method and found this thread very useful. Please let me to ask a couple of questions also:
- What is the difference between yield() and sleep()? I think the sleep() forces the current thread to stop, but the yield() doesn't.
- When does the thread return working after yield() is invoked? Does it depend on the jvm?