O'Reilly logo

Java 7 Concurrency Cookbook by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required