Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #2404
| Newsgroups | comp.lang.java.help |
|---|---|
| Date | 2013-01-01 15:43 -0800 |
| References | <kbvhvc$le1$1@dont-email.me> <kbvnbq$mgr$1@dont-email.me> |
| Message-ID | <76918f02-7c44-486d-a41d-083a07dca174@googlegroups.com> (permalink) |
| Subject | Re: Safety Of Non-Synchronized Collections |
| From | Lew <lewbloch@gmail.com> |
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
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Safety Of Non-Synchronized Collections Steve <tinker123@gmail.com> - 2013-01-01 15:46 -0500
Re: Safety Of Non-Synchronized Collections Knute Johnson <nospam@knutejohnson.com> - 2013-01-01 14:18 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-01 15:43 -0800
Re: Safety Of Non-Synchronized Collections markspace <markspace@nospam.nospam> - 2013-01-01 16:17 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-02 11:12 -0800
Re: Safety Of Non-Synchronized Collections markspace <markspace@nospam.nospam> - 2013-01-02 12:56 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-02 17:46 -0800
Re: Safety Of Non-Synchronized Collections Roedy Green <see_website@mindprod.com.invalid> - 2013-01-03 05:59 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-03 15:30 -0800
Re: Safety Of Non-Synchronized Collections Roedy Green <see_website@mindprod.com.invalid> - 2013-01-09 12:17 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-09 12:56 -0800
Re: Safety Of Non-Synchronized Collections Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-09 13:10 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-09 14:51 -0800
Re: Safety Of Non-Synchronized Collections Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-09 15:22 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-09 16:26 -0800
Re: Safety Of Non-Synchronized Collections Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-09 16:42 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-09 16:54 -0800
Re: Safety Of Non-Synchronized Collections Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-10 10:30 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-10 10:58 -0800
Re: Safety Of Non-Synchronized Collections markspace <markspace@nospam.nospam> - 2013-01-09 16:50 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-09 16:59 -0800
Re: Safety Of Non-Synchronized Collections markspace <markspace@nospam.nospam> - 2013-01-10 08:18 -0800
Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-10 10:49 -0800
Re: Safety Of Non-Synchronized Collections Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-01-10 13:37 +0200
Re: Safety Of Non-Synchronized Collections Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-10 09:27 -0500
Re: Safety Of Non-Synchronized Collections Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-01-10 17:14 +0200
Re: Safety Of Non-Synchronized Collections Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-10 11:44 -0500
Re: Safety Of Non-Synchronized Collections Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-01-10 23:41 +0200
Re: Safety Of Non-Synchronized Collections Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-10 17:14 -0500
Re: Safety Of Non-Synchronized Collections Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-01-11 09:10 +0200
Re: Safety Of Non-Synchronized Collections Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-11 08:25 -0500
csiph-web