yield() make the currently running thread head back to runnable to allow other threads to get their turn.
A thread can be moved out of the virtual CPU by yielding.
A thread that has yielded goes into the Ready state.
The yield() method is a static method of the Thread class.
It always causes the currently executing thread to yield.
class MyThread extends Thread {
public MyThread() {
setDaemon(true);
}
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Counting: " + i);
}
}
}
public class MainClass {
public static void main(String[] argv) {
MyThread ct = new MyThread();
ct.start();
Thread.yield();
}
}
Counting: 1
Counting: 2
Counting: 3
Counting: 4
Counting: 5
Counting: 6
Counting: 7
Counting: 8
Counting: 9
Counting: 10
7.6.Yielding |
| 7.6.1. | Controlling Threads: Yielding |