Java Glossary : synchronized

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : S words : synchronized.

synchronized
a crucial method that must not be executed by two threads simultaneously. Note the American spelling that all Java terms use. A crucial block of code you don't want to threads accessing simultaneously can be designed to allow only one thread through at a time, using:

synchronized (someObject) { /* crucial code */ }

You always need to specify some common object upon which the locks for all the threads can wait.

Only one thread at a time can own the lock for any given object. Thus if more than one thread tries to enter a section of code for which the lock must be acquired then only one thread will get the lock and all other threads will block. Note however that a thread can still execute a non-synchronized method or block of code even if the object is locked.

According to David Holmes, a synchronized non-static method locks the object to which the method belongs, on entry to the method and unlocks it on exit (well, if it already owns the lock - methods are reentrant - it actually increases and decreases the lock count respectively). The lock keeps all synchronized methods out of the object, not just other calls to that particular method. A statement:

synchronized (someObject) { /* crucial code */ }

locks the object someObject on entry to the statement and unlocks it on exit (or increases/decreases the count if its already owned). A synchronized instance method e.g.

synchronized doit() { /* crucial code */ }

is syntactic shorthand for:

doit() { synchronized(this){ /* crucial code */ } }

To protect access to static variables you must lock the class object - which is what a synchronized static method does. Alternatively in any method you can obtain a lock on the class object explicitly using

synchronized (getClass()) { /* crucial code */ }

When multiple threads are waiting to acquire the lock on an object the order in which the lock is assigned to a waiting thread is not defined.

One simple application would be queue with multiple threads adding work to be done to it, and multiple threads removing packets of work to do from it. So long as you synchronised the get and put methods, all threads can share the same queue safely.

In many applications is safe to have many reader threads simultaneously accessing a data structure, but only one writer thread. If there is a writer thread active, then all other reader and writer threads must be blocked. Arranging this is somewhat trickier that locking the entire datastructure for every read/write.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 12590.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/synchronized.html J:\mindprod\jgloss\synchronized.html