I have the following code:
public class MyThread extends Thread {
private int i;
public static int sum=0;
public MyThread(int k){
i=k;
}
The synchronized
keyword there prevents synchronized method calls on the same object from being interleaved. It does not prevent interleaving method calls on different objects. Since you have three different objects, the three calls can run simultaneously.
You need to synchronize on a single object which is shared by all three threads.