Re: Safety Of Non-Synchronized Collections
Knute Johnson wrote:
Steve wrote:
I was reading a thread about using StringBuilder versus StringBuffer.
The bottom line seemed to be that the two collections were the same
They aren't collections.
thing except that a StringBuffer is synchronized, is a thread safe and
It has synchronized methods. That doesn't make it thread safe.
does a little less well in performance as compared to StringBuilder
which are not those things.
StringBuilder is recommended. You pretty much will never need to share it
between threads.
Someone brought up how a similar relationship existed between ArrayList
and Vector.
In the limited respect that Vector's methods are synchronized and those of
ArrayList are not, the situation is similar. In other respects it is quite
different.
Is it a good (SAFE) rule of thumb to NOT use synchronized collections (
or anything else ) unless I am consciously creating multiple threads?
What's your opinion?
Rules of thumb are, by definition, limited. You need to know when to apply
them and when not to. Absent that, no rule of thumb is good or safe.
You spelled "SAFE" in all caps. Why? Does that mean it's more important to
you than other considerations?
In other words, I got the message "Use the new StringBuilder wherever
possible.", what I want to know is, how can I be sure it is possible?
Never share it between threads. Then it's always possible.
If you have to share it between threads, don't. Don't use StringBuffer either.
Share Strings between threads. They're (effectively) immutable.
That makes them safe.
So build your String and share the result. Don't share the builder.
Am I safe using non-synchronized collections as long as I don't create
new threads intentionally?
You are never safe using anything you don't understand.
You aren't even safe using synchronized collections in a multi-threaded
context. Also, you don't use Vector when you need a synchronized collection,
except when interacting with legacy code that doesn't recognize 'List'.
Your question is really too complicated to answer simply. I suggest
Well, the simple rule of thumb is "Don't use StringBuffer. Don't use Vector."
The better rule (not of thumb) is "Know thine API."
your buy and read the best book on the subject "Java Concurrency In
Practice" by Goetz et al. It is the definitive book at this point.
+1
For a simple rule of thumb however, if you only access an object's
fields or methods from one thread, no synchronization is necessary.
That is known as thread constraint and is one way to not have to do any
external synchronization.
Another rule of thumb is declare final references to immutable instances.
If you don't change the value after you hit a synchronization boundary, you
don't have to synchronize further.
You will find that some folks are a bit rabid about the Vector/ArrayList
and StringBuffer/Builder thing but in most circumstances you'll never
notice the difference.
Sure you will. StringBuilder is not spelled with a "Buffer", and "Vector"
appears nowhere in the "ArrayList" declaration.
Basically, don't use StringBuffer or Vector at all.
If you are certain that you absolutely must, and you understand all the
ramifications, you won't need my advice.
--
Lew