volatile Member Variable Modifier

Chapter 7 - Concurrent Access to Objects and Variables

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Speeding Concurrent Access
Synchronization is critical to writing multithread-safe code. Synchronization does come at a cost. The simple task of acquiring and releasing a lock adds more work for the processor and slows execution. This extra processing cost is why the methods in the Collections API are not synchronized by default. When only one thread is working with a collection, synchronization is a waste of processor resources. However, many of the classes can be wrapped in synchronization when access by two or more threads makes it necessary.
To speed up execution, do not use synchronized unnecessarily. Be sure that its really needed for proper functioning. If synchronization is necessary, see if using a synchronized statement block would work instead of a synchronized method. Although this wont decrease the cost of acquiring and releasing the lock, it will reduce contention for the lock among the other threads because the lock is held for a shorter period of time.
  Tip As you gain expertise in multithreaded programming, youll begin to have a better feeling for where synchronization is and is not required. As you start out, you should err on the side of using too much synchronization (but watch out for deadlocks!). This way, the code might be slower than it could be, but it will be less likely to be missing synchronization where it is critically needed.

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net