Hi Guys,
Thank you . ..Thank you very much.... you all should practice threads for at least these ways plus some other combination
1>
class a1 extends Thread
{
public static void main(String args[])
{
a1 a =new a1();
Thread a11 = new Thread(a);
Thread a12 = new Thread(a);
a11.run();
a12.run();
}
int x,y;
synchronized int method1()
{
x =x+method2();
return x;
}
synchronized int method2()
{
y =y+method1();
return y;
}
public void run()
{
method1();
method2();
System.out.println(x+""+y);
}
}
what will be the result?
answer..........
stack overflow
2 >class a1 extends Thread
{
public static void main(String args[])
{
a1 a =new a1();
Thread a11 = new Thread(a);
Thread a12 = new Thread(a);
a11.run();
a12.run();
}
int x=1,y=1;
synchronized void method1()
{
x =x+1;
y=y+1;
}
public void run()
{
for(int i =0 ; i<5;i++)
{
method1();
System.out.println(x+""+y);
}
}
}
What will be the pattern of values of x & y
Can you predict it?
or 2 2...3 3.... 4 4 ...
or 2 3 ..3 4.... 4 5..
answer...
Test and see........
bye everybody Thanks Shalini
Amit