Example of a guarded object inside core java as defined in JCIP book

What comes to my mind immediately is the list held by the SynchronizedList instance returned by Collections.synchronizedList(). Here's a part of its source code:



static class SynchronizedList
extends SynchronizedCollection
implements List {

final List list;

public boolean equals(Object o) {
synchronized (mutex) {return list.equals(o);}
}
public int hashCode() {
synchronized (mutex) {return list.hashCode();}
}

public ListIterator listIterator() {
return list.listIterator(); // Must be manually synched by user
}


The list object is not thread safe, but is guarded by the mutex. The object returned by listIterator() should be manually guarded by the same mutex (which is the synchronized list itself)


More: