Below code doesn't give synchronized output.
public class MultiThr implements Runnable{
public static void main(String[] args) {
for(int i = 0;i < 5;i++){
new Thread(new MultiThr()).start();
}
}
@Override
public void run() {
increment();
}
public synchronized void increment(){
for(int i=0;i<10;i++){
System.out.println(i);
try {
Thread.sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Could anybody explain me how to get object lock on increment() method?