I've come across code like the following several times
class Foo {
private Object lock = new Object();
public void doSomething() {
synchronized(lock) {
...
What I'm interested in is why a lock object is created instead of writing synchronized(this)
? Is it there to enable sharing the lock? I vaguely remember reading that it is an optimization. Is that true? Also, does it, in some context, make sense to have the lock declared as final
?