public class yield1 implements Runnable {
@Override
public void run() {
for (int x = 1; x <= 4; x++) {
if (x == 2) {
Thread.yield();
} else {
System.out.println(x + "Thread name is "
+ Thread.currentThread().getName());
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new yield1());
Thread t2 = new Thread(new yield1());
Thread t3 = new Thread(new yield1());
t1.start();
t2.start();
t3.start();
}
}
Output :
1Thread name is Thread-0 1Thread name is Thread-2 1Thread name is Thread-1 3Thread name is Thread-0 4Thread name is Thread-0 3Thread name is Thread-1 4Thread name is Thread-1 3Thread name is Thread-2 4Thread name is Thread-2If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.