O'Reilly logo

Java™ Phrasebook by Timothy Fisher

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 Threads

public synchronized void myMethod() {
											// do something
											}

You perform synchronization when you want to protect sections of code from being accessed by more than one thread at a time. The synchronized keyword, which we show in this phrase, allows us to synchronize a method or code block so that only one thread at a time can execute this method or code block. In this phrase, if one thread is currently executing myMethod(), any other threads attempting to execute the same method, myMethod(), on the same object instance will be locked out of the method until the current thread completes execution and returns from myMethod().

For non-static methods, the synchronization applies only to the object instance that another thread is ...

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