Code:
class A extends Thread {
public void run() {
Thread.yield();
System.out.println("Child Thread");
}
}
public class Human {
public static void main (String agrs[]) {
A t1 = new A();
A t2 = new A();
A t3= new A();
A t4= new A();
A t5= new A();
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
for(int a=0;a<1000;a++) {
System.out.println(a);
}
}
}
Output:
626
627
628
629
630
a's value
a's value
a's value
631
When I run my program the output is varied from run to run. Even when I use the yield()
method to let the main thread first finish it's job. But still now I get mixed outputs. Why?