Witscale Test Center

3.5 Method declaration and modifiers > 3.5.5 Synchronized methods


3.5.5 Synchronized methods

Earlier in this chapter when we discussed volatile variables (see section 3.4.3), we also discussed the two mechanisms that Java provides to control the access to shared data, synchronization and the volatile variables. In synchronization, it is made sure that only one thread will access the shared data (and also the shared code). You can achieve synchronization by using the synchronized modifier for the code (either a method or a code block) that accesses shared data. The method with synchronized modifier can thus be accessed by only one thread at a time. We will discuss this modifier in detail in chapter 12 (Threads). For the time being you only need to remember, that synchronized is an implementation-specific modifier and hence it is applicable only to methods and free-floating code blocks.