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 lock used by synchronized
methods is associated with an instance of class MyThread
. Since each thread has its own instance MyThread
, each thread is synchronizing on its own lock.
If you wanted to synchronize across all threads, you could do something along the lines of:
public class MyThread extends Thread {
private static final Object sharedLock = new Object();
public void doSomething() {
synchronized(sharedLock) {
for(int i=0; i<100000; i++) {
System.out.println(this.i);
}
}
}
...
}
An alternative is to synchronize
on MyThread.class
, but I prefer the first approach.
In the comments you say that if you change your code to use synchronized(this)
in doSomething
, all of a sudden it works. I am pretty sure it doesn't. It may have worked by chance, but it won't work repeatedly and reliably.
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.
The synchronization on methods only holds for invocations of the same object. You are creating two different objects (the two threads).