----- Original Message -----
From: Raymie Stata <stata@pa.dec.com>
To: <javaMemoryModel@cs.umd.edu>
Sent: Tuesday, June 29, 1999 8:12 AM
Subject: Re: JavaMemoryModel: Idiom for safe, unsynchronized reads
> Doug Lea writes "immutable objects are threadsafe." However, if I were a
> programmer sitting down to make coding decisions, then I wouldn't find
this
> to be a very instructive principle.
>
> Josh writes:
>
> "...synchronization is only for mutable variables, i.e., those that
> can be modified after they're 'first published.' [Synchronization
> is not needed for variables that are not modified after they are
> first published.]"
>
> Although this sounds simple on the surface, I think it is error-prone
> and that it has more subtleties than does my principle.
>
I wonder how these rules for accessing "immutable" variables apply when the
variable is declared by interface (or via a non-final class). Just because
the interface (or non-final super class) does not contain "set" operations
doesn't mean that the underlying object is immutable.
How can the user of these variables know when the immutable rules apply -
without more knowledge of the variable's contents? (Or do the guarantees
proposed concerning immutable objects apply only to the creators of
objects - and not to their users?)
> Now consider the following variation:
>
> // Version Z
> static String foo[];
> public String[] getFoo() {
> if (foo == null) {
> String[] tmp = new String[...];
> for (int i = 0; i < ...; i++) tmp[i] = new String(..);
> foo = tmp;
> }
> return foo;
> }
>
By the way, I think you can simplify this even more using "atomic" values in
the array:
static int[] foo;
public String[] getFoo() {
if (foo == null) {
int[] tmp = new int[] {1};
foo = tmp;
}
return foo;
}
(Is getFoo()[0] equal to 0 or 1? Depends...)
There's an interesting variant in javax.swing.event.EventListenerList. The
addListener() and removeListener() methods are synchronized, but the
getListenerList() method is not synchronized because it assumes that
"objects are as visible as their references".
-- Joe Bowbeer------------------------------- This is the JavaMemoryModel mailing list, managed by Majordomo 1.94.4.
To send a message to the list, email JavaMemoryModel@cs.umd.edu To send a request to the list, email majordomo@cs.umd.edu and put your request in the body of the message (use the request "help" for help). For more information, visit http://www.cs.umd.edu/~pugh/java/memoryModel
This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:14 EDT