Synchronizing a Collection or Map

The synchronized keyword is an important part of the subject of multithreading, a more complicated topic that will not be introduced until Chapter 13. Here, I shall note only that the Collections class contains a way to automatically synchronize an entire container. The syntax is similar to the unmodifiable methods:

In this case, you immediately pass the new container through the appropriate synchronized method; that way, theres no chance of accidentally exposing the unsynchronized version.

Fail fast

The Java containers also have a mechanism to prevent more than one process from modifying the contents of a container. The problem occurs if youre iterating through a container, and some other process steps in and inserts, removes, or changes an object in that container. Maybe youve already passed that object, maybe its ahead of you, maybe the size of the container shrinks after you call size( )there are many scenarios for disaster. The Java containers library incorporates a fail-fast mechanism that looks for any changes to the container other than the ones your process is personally responsible for. If it detects that someone else is modifying the container, it immediately produces a ConcurrentModificationException. This is the fail-fast aspectit doesnt try to detect a problem later on using a more complex algorithm.

Its quite easy to see the fail-fast mechanism in operationall you have to do is create an iterator and then add something to the collection that the iterator is pointing to, like this:

The exception happens because something is placed in the container after the iterator is acquired from the container. The possibility that two parts of the program could be modifying the same container produces an uncertain state, so the exception notifies you that you should change your codein this case, acquire the iterator after you have added all the elements to the container.

Note that you cannot benefit from this kind of monitoring when youre accessing the elements of a List using get( ).