I'm looking at java.util.Observable code and I noticed that
setChanged() is synchronized, however i can't figure out why... anyone
can help me?
protected synchronized void setChanged() {
changed = true;
}
A couple of remarks, non of which might apply:
(a) When studying synchronization (multithreading behavior), you have to
look at all methods who have access to the particular data, not only at
one isolated method. So there might (or might not) be some other code in
Observable that depends on setChanged() being synchronized.
(b) One can see such things in several of the original Java 1.0 classes,
e.g. the pre-collection classes like Hashtable. My guess is that the
original Java designers were a little bit naive when it came to
multithreading and synchronization. Synchronizing setChanged() might (or
might not) have been an overhasty design decision.
(c) Observer/Observable work as advertised, but are often just to simple
or to clumsy for real world tasks. If they don't cut it for you, do your
own.
/Thomas