Tuesday, October 30, 2007

Private lock Object Idiom

The private lock object idiom is generally used in making a class Thread Safe.
Instead of using the instance of the object as a lock object an internal private object is used
as a lock object.

private Object lock = new Object();

so the method need to acquire a lock should use the lock object instead of this object.
e.g.

public void foo(){
synchronized(lock){
....
}
}

No comments: