Optimization 45: Read/Write Locks Another way to ease
Key Points . SMP is currently the dominant MP architecture. It consists of multiple symmetric processors connected via a single bus to a single memory system. The bus is the scalability weak link in the SMP architecture. Large caches, one per processor, are meant to keep bus contention under control. . Amdhal’s law puts an upper limit on the potential scalability of an application. The scalability is limited by portions of the computation that are synchronized or otherwise single-threaded. . The straight-line execution of unsynchronized code is faster than synchronized code, even without contention. Synchronized code hides the cost of acquiring and releasing a lock associated with a class or object. . In the presence of thread contention, synchronized code could become a severe performance and scalability inhibitor. The trick to scalability is to reduce and, if possible, eliminate synchronized code. Following are some steps you can take towards that goal: . Division of labor. Split a monolithic task into multiple subtasks that are conducive to parallel execution by concurrent threads. . False sharing. If two class (or object) members are logically unrelated, don’t use the associated class (or object) lock to synchronize access. That will force the two unrelated data entities to share a lock, which increases contention. Protect access to those members using distinct locks. . Code motion. Synchronized code should only contain access to shared data and nothing else. Code that does not directly manipulate shared resources should not reside within the scope of synchronization. . Share nothing. If you need only a small, fixed number of resource instances, you should avoid the use of public resource pools. Make those instances private to the thread and recycle them. . Partial-sharing. It is better to have two identical pools with half the contention. . Reader/writer locks. Shared data that is read-mostly will benefit from these locks. They eliminate contention among reader threads. Page 175
Note: If you are looking for good and quality webspace to host and run your java application check Actions java hosting services