16.6.2 Synchronization
The mechanism by which access to shared resources by different threads is controlled is called Synchronization. While the synchronization functionality in Java is convenient and easy to use, it can introduce significant performance overhead. When a block of code is synchronized, only a single thread can execute it at any one time. There are two performance impacts of synchronization:
-
| Manage the monitors, the objects internal to the JVM that are used to control access to synchronized resources. Although they are not explicitly accessed by programmers, there is an overhead due to the management of the monitors by the JVM.
|
-
| Reduced concurrency, since threads have to wait for other threads to exit from synchronized blocks of code.
|
Thus the use of synchronization should be minimized and limited to cases where it is definitely necessary. It is also good practice to clearly document all assumptions relating to synchronization, because they may not be obvious to someone reading the design or code.
When using synchronization, it is best to use specific lock objects to synchronize on. Synchronizing using the keyword can cause different methods to be unnecessarily synchronized with each other, and hence reduce concurrency. Note that synchronizing on an object has a greater overhead than calling a synchronized method. However, synchronizing the method may result in significantly greater amounts of code being synchronized, again reducing the concurrency. So the trade-off between the synchronization overhead and reduced concurrency needs to be evaluated on a case-by-case basis.
In addition to the explicit use of synchronization in application code, synchronization may be used indirectly, as some of the commonly used core Java functionality uses synchronization. Some particular examples are:
-
| The Java I/O libraries. It is best to minimize the use of System.out.println() for this reason. Use of a multithreaded logging library as discussed in 16.6.3, Logging is suggested.
|
-
| Some of the Java collection classes, such as java.util.Hashtable and java.util.Vector, are synchronized. If only a single thread is accessing the data (or multiple threads are reading only), the synchronization overhead is unnecessary. Many of the newer collections introduced in Java 1.2, such as java.util.ArrayList are not synchronized and may provide better performance. However, care needs to be taken when accessing them from multiple threads.
|