You are here:

Java/yield method in java

Advertisement


Question
Hello sir!yield method makes currently executing thread to pause to make another active thread to run.
i got some doubts on yield method.i previously asked about yield.but this time some what differnent than previous.I am asking based on below code.
I hope you help me.
------------------------------------------------------------------
class thread7
{
public static void main(String args[])
{
Thread count1=new Thread(new CountDownEven());
Thread count2=new Thread(new CountDownOdd());

Thread t=Thread.currentThread();
count1.start();
count2.start();
for(int i=11;i<=20;i=i+2)
{
System.out.println(t.getName()+"----"+i);
Thread.yield();
}

}
}
class CountDownEven implements Runnable
{
public void run()
{
Thread t=Thread.currentThread();
for(int i=0;i<10;i=i+2)
{
System.out.println(t.getName()+"----"+i);
Thread.yield();
}
}
}
class CountDownOdd implements Runnable
{
public void run()
{
Thread t=Thread.currentThread();
for(int i=1;i<10;i=i+2)
{
System.out.println(t.getName()+"----"+i);
Thread.yield();
}
}
}
------------------------------------------------------------------
Mydoubts:
1)Does yield method guarantees that after executing yield control goes to another active thread?
see in the below output in line 3,4 thread 0 got executed two times.why this happened?Is it due to drawback of yield or due to cpu execution?
2)what are the drawbacks of yield method?
3)If it has drawbacks then why yield method?what is the purpose of yield?
------------------------------------------------------------------
output:
-------------
Thread-0----0
main----11
Thread-0----2
Thread-0----4
Thread-1----1
main----13
Thread-1----3
Thread-0----6
Thread-1----5
main----15
Thread-1----7
Thread-0----8
Thread-1----9
main----17
main----19
------------------------------------------------------------------

Answer
1. In theory, yes, but the other thread it goes to could not be in your program, and those other threads might be sleeping. So it is possible that a thread that yields gets to execute again before any of your other threads go. This is even more likely if your other threads are also yielding. In short, don't use yield to depend on the execution of thread order.
2 & 3. The purpose of yield is to temporarily pause in a calculation to ensure that not all of the system resources get used at once. If there is a higher priority thread that is running (say the thread managing user input), then it will probably (but not guaranteed) be able to run and catch up on operations like the user hitting a button.

In short, threads by themselves are not predictable. You must code that predictability in if you need it. Thread.yield() does not enable predictability.

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.