yield()
method in Java is used for current thread execution to give a chance for waiting for a thread of the same priority. If all waiting threads have low priority or there is no waiting thread then the current thread execution will continue.
If multiple threads are waiting with the same priority then with thread will chance of execution no one can decide exactly which Thread is going to get a chance for execution. It completely depends upon the Thread scheduler. The Thread execution which is yielded(), when it is again going to get a chance for execution depends upon the Thread scheduler.
public class YieldMethod extends Thread{
@Override
public void run() {
for(int i=0;i<10;i++){
Thread.yield();
System.out.println("Child Thread is executing");
}
}
}
class TheCodeData{
public static void main(String[] args) {
YieldMethod obj = new YieldMethod();
obj.start();
for(int i=0;i<4;i++){
System.out.println("Main Thread is executing");
}
}
}
Note-
Some platforms would not provide proper support for the yield() method.
Similar Java Tutorials
- Odd Even in Java
- Method to print exception information in Java
- Exception Handling by Using try-catch in Java
- Checked Exception vs Unchecked Exception
- Exception Hierarchy in Java
- Java Exception Handling Interview Questions
- final finally finalize in java
- User Defined Exception in Java
- Exception Handling Keyword in Java
- throw throws in Java
- try with multiple catch block in Java
- Threads in Java
- Thread priority in Java