Home »
Java programming language
Java Thread Class static native void yield() method with Example
Java Thread Class static native void yield() method: Here, we are going to learn about the static native void yield() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 29, 2019
Thread Class static native void yield()
- This method is available in package java.lang.Thread.yield().
- yield() method says to stop the currently executing thread and will give a chance to other waiting threads of the same priority.
- If in case there are no waiting threads or if all the waiting threads have low priority then the same thread will continue its execution.
- The advantage of this method is to get a chance to execute other waiting threads so if our current thread takes more time to execute and allocate processor to other threads.
- This method is static so we can access this method with the class name too.
- This is a native method that means the implementation of this method is available in other languages like C, C++ so whenever we need this method so we can declare in our class.
- We can't expect when yielded will get a chance it is decided by the Thread Scheduler.
- The return type of this method is void so it does not return anything.
Syntax:
static native void yield(){
}
Parameter(s):
We don't pass any object as a parameter in the method of the Thread.
Return value:
The return type of this method is void, it does not return anything.
Java program to demonstrate example of yield() method
/* We will use Thread class methods so we are
importing the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class MyThread extends Thread {
// Override run() method of Thread class
public void run() {
for (int i = 0; i < 5; ++i) {
// By calling yield() method means MyThread stop its
// execution and giving a chance to main thread
Thread.yield();
System.out.println("Thread started:" + Thread.currentThread().getName());
}
System.out.println("Thread ended:" + Thread.currentThread().getName());
}
}
class Main {
public static void main(String[] args) {
// Here we are calling start() method of Thread class and
// it will call run() method of MyThread
MyThread mt = new MyThread();
mt.start();
for (int i = 0; i < 5; ++i) {
System.out.println("Thread started:" + Thread.currentThread().getName());
}
System.out.println("Thread ended:" + Thread.currentThread().getName());
}
}
Note:
- If we comment this line /*Thread.yield()*/ then both thread will execute simultaneously we can't expect which thread will complete its execution.
- If we don't comment this line /*Thread.yield()*/ then there is a possibility to execute main thread first because MyThread always calls yield() method.
Output
E:\Programs>javac Main.java
E:\Programs>java Main
Thread started:main
Thread started:Thread-0
Thread started:main
Thread started:main
Thread started:main
Thread started:main
Thread started:Thread-0
Thread ended:main
Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread ended:Thread-0