This is the final article in this series of “Effective Java – Key Points”. Let’s jump into the points.
- Synchronization is required for reliable communication between threads as well as for mutual exclusion. Synchronization will not have effect unless both read and write are synchronized.
- If only inter-thread communication is required and not mutual exclusion, then volatile modifier can be used.
- Avoid excessive synchronization. Never call an alien method from a synchronized region, to avoid any deadlock or data corruption as an alien method are designed to be subclassed and the API will never know what the implementation of those methods is.
- Never use newCachedThreadPool in a highly loaded environment because it as good as creating as many threads as requested. It is ideal to use newFixedThreadPool which could be optimized based on the server capacity.
- Lazy initialization holder class idiom and Double check idiom should be used in case of lazy initialization of threaded environment.