CODE FOR SYNCHRONIZATION - JOIN | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Tuesday, May 20, 2014

CODE FOR SYNCHRONIZATION - JOIN



PROGRAM:

class Maxthread extends Thread
{
int d[ ],index;
public Maxthread(int t[ ],int i)
{
d=t;
index=i;
}
public void run()
{
int max=d[0];
for(int i=1;i<d.length;i++)
if(d[i]>max)
max=d[i];
try
{
Thread.sleep(1000);   
}
catch(Exception e)
{
}
Joineg.v[index]=max;
System.out.println("\nMaximum of "+index+" Array is::"+max);
}
}
class Maxmax extends Thread
{
Thread a,b,c;
Maxmax(Thread t1,Thread t2,Thread t3)
{
a=t1;
b=t2;
c=t3;
}
public void run()
{
try
{
a.join();
b.join();
c.join();
}
catch(Exception e)
{
}
int m=Joineg.v[0];
for(int i=1;i<Joineg.v.length;i++)
{
if(Joineg.v[i]>m)
m=Joineg.v[i];
}
System.out.println("\n\nMaximum of all Three Array is::"+m);
}
}
class Joineg
{
static int v[ ]=new int[3];
public static void main(String arg[ ])
{
int x[ ]={125,256,302,14,436};
int y[ ]={347,137,618,198,1010};
int z[ ]={27,101,113,213,513};
Maxthread t,t2,t3;
t=new Maxthread(x,0);
t2=new Maxthread(y,1);
t3=new Maxthread(z,2);
Maxmax t1=new Maxmax(t,t2,t3);
t.start();
t1.start();
t2.start();
t3.start();
}
}



OUTPUT:


Maximum of 0 Array is::436

Maximum of 1 Array is::1010

Maximum of 2 Array is::513



Maximum of all Three Array is::1010

No comments:

Post a Comment

Thanks for your valuable comments

Followers