synchronized : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

synchronized
Note the American spelling synchronized. A crucial method that must not be executed by two threads simultaneously. Note the American spelling, synchronized, that all Java terms use. A crucial block of code you don’t want two 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. However, the attribute synchronized is not applied to the variable, but to the method or code block that accesses the Object. Further, understand you cannot lock on a primitive variable such as an int, only an Object (or some subclass).

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.

But that is not quite accurate either. According to David Holmes, a synchronized instance 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 */
   }

locks on this. It is 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.

You call the wait method, you must have a lock on the synchronising object. To use notify, you don’t. Almost never should you be fooling around with low level synchronisation tools like synchronized, volatile, wait and notify. They are extremely tricky to use correctly. Instead use the higher level tools in java.util.concurrent.

Books

book cover recommend book⇒Java Concurrency in Practiceto book home
by Brian Goetz, Tim Peierls, Joshua J. Bloch, Joseph Bowbeer, David Holmes, Doug Lea 978-0-321-34960-6 paperback
publisher Addison-Wesley 978-0-13-270225-6 eBook
published 2006-05-19 B004V9OA84 kindle
Bloch and Lea especially have very good reputations in concurrent programming. This is the dream team to write such a book.
Australian flag abe books anz abe books.ca Canadian flag
German flag abe books.de amazon.ca Canadian flag
German flag amazon.de Chapters Indigo Canadian flag
Spanish flag amazon.es Chapters Indigo eBooks Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Nook at Barnes & Noble American flag
Italian flag amazon.it Kobo American flag
India flag junglee.com Google play American flag
UK flag abe books.co.uk O’Reilly Safari American flag
UK flag amazon.co.uk Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.
book cover recommend book⇒Concurrent Programming in Java(TM): Design Principles and Patterns, third editionto book home
by Douglas Lea 978-0-321-25617-1 paperback
publisher Addison-Wesley
published 2006-05-19
Threads and concurrency in Java, design considerations (safety, liveness and performance), Before/After Patterns, layering, adapters, immutability and synchronization, deadlock, resource ordering, the Java Memory Model and concurrency, using the java.concurrency package, confinement, refactoring for concurrency, mutexes, read-write locks, recovering from failure, notifications, semaphores, latches, exchanges, transactions, one-way messages, worker threads, polling and event-driven I/O, parallelism techniques (fork/join, computation trees and barriers), Communicating Sequential Processes (CSP).
Australian flag abe books anz abe books.ca Canadian flag
German flag abe books.de amazon.ca Canadian flag
German flag amazon.de Chapters Indigo Canadian flag
Spanish flag amazon.es Chapters Indigo eBooks Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Nook at Barnes & Noble American flag
Italian flag amazon.it Kobo American flag
India flag junglee.com Google play American flag
UK flag abe books.co.uk O’Reilly Safari American flag
UK flag amazon.co.uk Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.
book cover recommend book⇒Thinking In Java, fourth editionto book home
by Bruce Eckel 978-0-13-187248-6 paperback
birth 1957-07-08 age:60
publisher Prentice Hall
published 2006-02-20
read online. Good if you want to understand the philosophy behind Java. It does not cover Java 1.5 or later, e.g. enums, generics, for:each loops, annotations.
Australian flag abe books anz abe books.ca Canadian flag
German flag abe books.de amazon.ca Canadian flag
German flag amazon.de Chapters Indigo Canadian flag
Spanish flag amazon.es Chapters Indigo eBooks Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Nook at Barnes & Noble American flag
Italian flag amazon.it Kobo American flag
India flag junglee.com Google play American flag
UK flag abe books.co.uk O’Reilly Safari American flag
UK flag amazon.co.uk Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.
book cover recommend book⇒Java Threads, third editionto book home
by Scott Oaks, Henry Wong 978-0-596-00782-9 paperback
publisher O’Reilly recommended 978-1-4493-6666-7 eBook
published 2004-09-10 B00BIRRRZA kindle
An introductory book with good examples. Not as deep as you would usually expect from O’Reilly.
Australian flag abe books anz abe books.ca Canadian flag
German flag abe books.de amazon.ca Canadian flag
German flag amazon.de Chapters Indigo Canadian flag
Spanish flag amazon.es Chapters Indigo eBooks Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Nook at Barnes & Noble American flag
Italian flag amazon.it Kobo American flag
India flag junglee.com Google play American flag
UK flag abe books.co.uk O’Reilly Safari American flag
UK flag amazon.co.uk Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.
book cover recommend book⇒Taming Java Threadsto book home
by Allen Holub 978-1-893115-10-1 paperback
birth 1955 age:62 B001GMAPVA kindle
publisher Apress
published 2000-06-01
Australian flag abe books anz abe books.ca Canadian flag
German flag abe books.de amazon.ca Canadian flag
German flag amazon.de Chapters Indigo Canadian flag
Spanish flag amazon.es Chapters Indigo eBooks Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Nook at Barnes & Noble American flag
Italian flag amazon.it Kobo American flag
India flag junglee.com Google play American flag
UK flag abe books.co.uk O’Reilly Safari American flag
UK flag amazon.co.uk Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.
book cover recommend book⇒Java Thread Programmingto book home
by Paul Hyde 978-0-672-31585-5 paperback
publisher Sams
published 1999-08-30
Australian flag abe books anz abe books.ca Canadian flag
German flag abe books.de amazon.ca Canadian flag
German flag amazon.de Chapters Indigo Canadian flag
Spanish flag amazon.es Chapters Indigo eBooks Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Nook at Barnes & Noble American flag
Italian flag amazon.it Kobo American flag
India flag junglee.com Google play American flag
UK flag abe books.co.uk O’Reilly Safari American flag
UK flag amazon.co.uk Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.

Learning More

Oracle’s Javadoc on java.util.concurrent package : available:
Oracle’s Javadoc on java.util.Queue class : available:
Oracle’s Javadoc on notify package : available:
Oracle’s Javadoc on wait package : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/synchronized.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\synchronized.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[18.97.14.86]
You are visitor number