While running the following code random results are coming. Random results are like:
While I run the code the result 1st came out was
B=1, B=2, B=3, B=4, B=5, exit from B, A=1, A=2, A=3, A=4, A=5, exit from A.
Next time
B=1, B=2, A=1, A=2, A=3, A=4, A=5, B=3, B=4, A=5, exit from B, exit from A.
Why?
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==3) yield();
System.out.println("A="+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("B="+j);
}
System.out.println("Exit from B");
}
}
class T
{
public static void main(String args[])
{
A obj1=new A();
B obj2=new B();
obj1.start();
obj2.start();
}
}