synchronization of methods(Topic-Threads)
Posted: Sun Feb 10, 2013 3:56 am
class Pt extends Thread
{
StringBuffer j = new StringBuffer();
public synchronized void run()
{
for(int i =0;i<2;i++)
System.out.println(j);
j.append("b");
}
Pt(StringBuffer j)
{
this.j=j;
}
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("a") ;
Pt t1 = new Pt(s);
Pt t2 = new Pt(s);
Pt t3 = new Pt(s);
t1.start();
t2.start();
t3.start();
}
}
what is wrong with this code? why isn't it behaving the same. I mean it should always produce 2"a"s , 2"ab"s and 2"abb"s. But it isn't behaving the same.
help me
{
StringBuffer j = new StringBuffer();
public synchronized void run()
{
for(int i =0;i<2;i++)
System.out.println(j);
j.append("b");
}
Pt(StringBuffer j)
{
this.j=j;
}
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("a") ;
Pt t1 = new Pt(s);
Pt t2 = new Pt(s);
Pt t3 = new Pt(s);
t1.start();
t2.start();
t3.start();
}
}
what is wrong with this code? why isn't it behaving the same. I mean it should always produce 2"a"s , 2"ab"s and 2"abb"s. But it isn't behaving the same.
help me