This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
However I think that in many cases where you do need synchronization somewhere in your code, the wrapper doesn't provide the sync in the right place. Consider:
Now let's say list is shared by multiple threads, so you attempt to protect it by wrapping it with Collections.synchronizedList() before passing it to this method. Does that make this code thread-safe? No. The sync wrapper synchronized each individual method call - but it doesn't protect you from other threads slicing in between synchronized method calls. In the above code, what if another thread removes an element from the list after size() is called, but before get(i) is called? You could have a runtime exception as the index is out of bounds. To protect you code propertly here, you'd need to sync at a higher level:
In general, I think that the Collections.synchronizedXXX() methods are of very limited use, and often give a false sense of security. Much like Vector and Hashmap do. If you need synchronization, you are usually better off putting it in yourself, I think. [ January 23, 2005: Message edited by: Jim Yingst ]