How to Avoid or Minimize Synchronization in java



    Avoid or Minimize Synchronization


    Many performance studies have shown a high performance cost in using synchronization in Java. Improper synchronization can also cause a deadlock, which can result in complete loss of service because the system usually has to be shut down and restarted.
     But performance overhead cost is not a sufficient reason to avoid synchronization completely. Failing to make sure your application is thread-safe in a multithreaded environment can cause data corruption, which can be much worse than losing performance.

     The following are some practices that you can consider to minimize the overhead:
    • Synchronize Critical Sections Only
    • Do Not Use the Same Lock on Objects That Are Not Manipulated Together
    • Use Private Fields
    • Use a Thread Safe Wrapper
    • Use Immutable Objects
    • Know Which Java Objects Already Have Synchronization Built-in
    • Do Not Under-Synchronize

    Comments