Hi Dale,
> You seem to have a lot to learn about what it really means to be "thread
> safe". Yes most of the API is not thread safe in that it is not safe for
> multiple threads to be calling methods on an instance without some sort
> of synchronization mechanism. That is not a bad thing.
>
> Thread safety does not begin and end with the Java API. Even if every
> method in the Java API were synchronized that would not in and of itself
> guarantee much of anything about the thread safety of any program that
> used those API's. Thread safety involves how an API is used as well.
>
> Consider if I had a class that maintained its state using a Vector and a
> Hashtable. Both of those classes are thread safe in that you can make
> calls on an instance from multiple threads and the internal state of the
> Hashtable or Vector will be consistent. But in my class that uses them
> if I add an item it needs to be added to the Hashtable and Vector. If I
> don't do synchronization within my class then I can still end up with an
> inconsistent state between the Hashtable and Vector.
>
> Since most thread safety issues involve big picture issues like this it
> is rather pointless (and quite harmful from a performance standpoint) to
> have the low level API's try to be thread safe (and giving users a false
> sense of security). Most of the time an API will only be accessed from
> one thread any way. They therefore elect to put the burden on the user
> to determine the best way to make it thread safe, but provide some
> optional low level thread safety on the very simple classes like the
> collection classes and StringBuilder vs. StringBuffer.
I understand that we cannot just take an API that is marked "thread safe"
and blindly use it; it is essential that we look at how it is used from a
higher level. I also understand that it can make sense to push the
responsibility for thread-safety to the calling client level for perfomance
raesons as well. However, if we are starting with an API that *may or may
not* itself be thread-safe" aren't we having to deal with a greater level of
uncertainty than is necessary than if the documentation made a clear
statement? For example, the IOImage class is not documented as being safe
or unsafe. However, we know that a static member such as
IOImage.createImageInputStream() is static so that means (as I understand
it) that it manipulates at least one Class variable. If my thread is in the
middle of executing createImageInputStream() and another thread enters this
function don't I need to be concerned?
I just realized that in the example I just gave that I mixed two of my
problems/areas of concern which are:
1) Can you speculate on why Sun wouldn't make a statement like "none of the
API is thread-safe unless we state otherwise" so we can know with certainity
that we are building a block of code that uses an API that is itself either
thread-safe or not
2) In any case, how can we synch on a J2SE/EE static method such as
createImageInputStream()? I can ensure that all of *my* code (and hence
threads) are synched, but since this API is available to any other
application running in the JVM there's no way I can ensure that some other
thread won't interrupt my thread in the middle of such a static method. Are
we reduced in this situation to having to obtain and study the API source
itself to see if we have a problem?
Thanks,
Al,