explain yielding in threading?

haneefnb
haneefnb used Ask the Experts™
on
Hi
can u explain yielding in threading..and how can we do this. give me a example
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
See java.lang.Thread.yield()

It is simple the act of "yielding" your CPU time / execution priority to others.

Same as in traffic, if you yield to another driver, you are voluntarily offering the right of way to the other driver.
Top Expert 2009

Commented:
When one thread yields, another thread is scheduled (if there is another one waiting).
Software Developer
Commented:
Generally speaking, in a multithreaded execution environment the scheduling can be preemptive or non-preemptive.  With preemption a thread can be removed from execution and be placed in a ready-to-run queue in favor of another thread according to some preemption policy in the scheduler.  With non-preemption a thread will continue to execute unless it does something that causes it to give up the processor.  This can take the form of requesting I/O upon which it must wait or even explicitly giving up the processor.

A "well-behaved" thread should refrain from tying up the processor it is running on for too long.  In a preemptive environment, this is already taken care of.  In a non-preemptive environment it is up to the program logic to play the good citizen.  So if a thread is computing the inverse of a very large matrix (something that will take a long time and not block for I/O or some other external resource) in a non-preemptive environment, it would voluntarily yeild after every iteration of one of the outer loops thereby ensuring that it does not hog the processor and starve the other waiting threads.

Most Java implementations implement some form of preemptive scheduling of threads, but this is not a required behavior of JVMs.  Java threads exist in either the BLOCKED, RUNNABLE, or RUNNING states.  A BLOCKED thread is not eligible for execution due to some external event or resource that has not yet available.  A RUNNABLE thread is eligible for assignment to a processor but is still waiting assignment and therefore not running.  A RUNNING thread is one that is actually executing. (Actually there are additional states - see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.State.html)

Java threads may implicitly yield due to requesting I/O for which there will be some latency of response (making them BLOCKED until the I/O completes at which time they become RUNNABLE) or they may yield explicitly (placing them in a state that depends on how exactly they yielded.

In addition to java.lang.Thread.yield() suggested by mrjoltcola above; also see the sleep methods in that same class.
Note that the yield behaviour is not guaranteeing anything. It all depends on your JVM. So it is true at one time, the thread that is running has the highest priority but nothing is guaranteed.

Author

Commented:
Thanks...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial