View Single Post
  #16 (permalink)  
Old 07-22-2008, 10:31 AM
JPRABHU JPRABHU is offline
Member
 
Join Date: Jul 2008
Posts: 3
JPRABHU is on a distinguished road
public class MyAnswer
{
public static void main(String[] args) {
Thread_1 aaa = new Thread_1("AAAAAAAAAAAAAAA");
Thread_2 bbb = new Thread_2("BBBBBBBBBBBBBBB");
Thread x = new Thread(aaa);
Thread y = new Thread(bbb);
x.start();
y.start();



}
}



class Thread_1 implements Runnable {
String str;
public Thread_1(String s) { str = s; }



public void run()
{
synchronized(Thread_2.class)//-----> acquired lock on class

{
int n = 0;
while (n<100) {
System.out.print(str);
System.out.println();
n++;
}

}
}
}

class Thread_2 implements Runnable {
String str;
public Thread_2(String s) { str = s; }

public void run() {
synchronized(Thread_2.class)//-----> acquired lock on class
{
int n = 0;
while (n<100) {
System.out.print(str);
System.out.println();
n++;
}

}
}
}



According to me, If we acquire lock on a class is the better solution
Reply With Quote