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();
}
}
|