I was reading a threading tutorial that’s originally from (I believe) the IBM developerworks site. In it they talked about the synchronized keyword and how a synchronized block of code is locked by the actual object, not the block of code itself.
For instance, in the code below the authors state that even though the static class ‘Thingie
’s setLastAccess
method is listed as synchronized, the two threads defined below it can each call setLastAccess
simultaneously, because they’re using different values for thingie. But if thingie is static won’t that mean they’re using the same value?
Is it the case that the variable names only need be different, even if they're referring to the same object?
public class SyncExample {
public static class Thingie {
private Date lastAccess;
public synchronized void setLastAccess(Date date) {
this.lastAccess = date;
}
}
public static class MyThread extends Thread {
private Thingie thingie;
public MyThread(Thingie thingie) {
this.thingie = thingie;
}
public void run() {
thingie.setLastAccess(new Date());
}
}
public static void main() {
Thingie thingie1 = new Thingie(),
thingie2 = new Thingie();
new MyThread(thingie1).start();
new MyThread(thingie2).start();
}
}