Skip to main content

Posts

Showing posts with the label synchronized statement

Thread Synchronization

    In Java, thread synchronization is achieved through two constructs: synhcronized methods and synhcronized statements. int a = 7 ; void synchronized commonMethod (){     a += 5 ; // additional processing of data not shared among threads       // but unnecessarilly synchronized } An example of synchronized methods can be seen above. int a = 7 ; void commonMethod (){     synchronized ( this ){         a += 5 ;     }         // additional processing of data not shared among threads.         // Unnecessary synchronization is avoided. } Code fragment above is an example of synchronized statements. Note that synchronized methods may lead to unnecessary synchronization of code segments, while synchronized statements has the flexibility to avoid those situat...