public class B extends Thread {
@Override
public void run() {
print();
}
public synchronized void print(){
int i;
for (i=0;i<1000;i++){
System.out.println(Thread.currentThread().getName() );
}
}
}
public class A {
public static void main(String[] args) {
B b1=new B();
B b2=new B();
b1.start();
b2.start();
}
}
How to lock the print method acessing two objects of the class B ? What I am wanting is here I ve synchronized the method there is no use of it ! so I want the thread 1 to run the print method 1st and then thread 2 . How can I change the code?