I've written this code to obtain an alternate display of '|' and '*' starting with '|'. To ensure the altering of o/p I've used the yield(). But this doesn't seem to serve the purpose. Can someone point out where have I gone wrong ?
import java.util.*;
class Pat extends Thread
{
static int cnt=0;
}
class Pattern1 extends Pat
{
public void run()
{
for(int i=1;i<=8;i++)
{ try{
System.out.print("|");
cnt=1;
if(cnt==1)
this.yield();
sleep(1000);
}catch(Exception e){}
}
}
}
class Pattern2 extends Pat
{
public void run()
{
for(int i=1;i<=7;i++)
{ try{
System.out.print("*");
cnt=0;
if(cnt==0)
this.yield();
sleep(1000);
}catch(Exception e){}
}
}
}
class Pattern
{
public static void main(String args[])
{
Pattern1 p1=new Pattern1();
Pattern2 p2=new Pattern2();
p1.setPriority(2);
p2.setPriority(1);
p1.start();
p2.start();
}
}