class One{
synchronized void display(int num){
System.out.println(""+num);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){
System.out.println("Error : "+e);
}
System.out.println("Done");
}
}
class Two implements Runnable{
int number;
One objo;
Thread objo1;
public Two(One one_num,int num){
objo=one_num;
number=num;
objo1=new Thread(this);
objo1.start();
}
public void run(){
objo.display(number);
}
}
class SynchMethod{
public static void main(String a[]){
One objo=new One();
int digit=10;
Two objSynch1=new Two(objo,digit++);
Two objSynch2=new Two(objo,digit++);
Two objSynch3=new Two(objo,digit++);
//wait for THreads to end
try
{
objSynch1.objo1.join();
objSynch2.objo1.join();
objSynch3.objo1.join();
}
catch(InterruptedException ex){
System.out.println("Error : "+ex);
}
}
}
No comments:
Post a Comment