In this recipe, we will learn how to use one of the most basic methods for synchronization in Java, that is, the use of the
synchronized keyword to control the concurrent access to a method. Only one execution thread will access one of the methods of an object declared with the
synchronized keyword. If another thread tries to access any method declared with the
synchronized keyword of the same object, it will be suspended until the first thread finishes the execution of the method.
In other words, every method declared with the
synchronized keyword is a critical section and Java only allows the execution of one of the critical sections of an object.
Static methods have a different behavior. Only one execution thread will ...