Cover by Javier Fernández González

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Synchronizing a method

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 ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required