Java Tutorials - Herong's Tutorial Examples - Version 7.00, by Dr. Herong Yang

"synchronized" - How Java Supports Synchronization

This section describes the 'synchronized' keyword and how Java supports synchronization in 3 different ways - synchronized class methods, synchronized instance methods, synchronized statement blocks.

Instead of let the programmers to design their own locks, manage the synchronization blocks, and apply the synchronization rules, Java offers a synchronization monitor on each instance of the Object class, so it can be used as a synchronization lock. Since all classes are sub classes of Object, all objects in Java can be used as synchronization locks.

Java also offers three ways to define synchronized blocks.

Synchronized Class Method:

class class_name {
   static synchronized type method_name() {
      statement block
   }
}

All the statements in the method become the synchronized block, and the class object is the lock.

Synchronized Instance Method:

class class_name {
   synchronized type method_name() {
      statement block
   }
}

All the statements in the method become the synchronized block, and the instance object is the lock.

Synchronized Statement:

class class_name {
   type method_name() {
      synchronized (object) {
         statement block
      }
   }
}

All the statements specified in the parentheses of the synchronized statement become the synchronized block, and the object specified in the statement is the lock.

Java applys the synchronization rule by assigning the ownership of the lock's monitor to the threads that are running the synchronized blocks. Here is how it works:

  • When a synchronized clock is reached in an execution thread, it will try to gain the ownership of the monitor of the lock object. If another thread owns the lock's monitor, it will wait.
  • Once the lock's monitor is free, the waiting thread will become the owner of the lock's monitor, and start to execute the synchronized block.
  • Once the synchronized block is executed to the end, the lock's monitor will be freed again.

Note that one program can have many locks and each lock can be associated with many different synchronized blocks. But the synchronization rule only applies between the synchronized block and its associated lock.

For example, the following code defines two synchronized blocks. Both are associated with the same lock, the instance object.

class class_name {
   type method_name() {
      synchronized (this) {
         statement block 1
      }
   }
   synchronized type method_name() {
      statement block 2
   }
}

Block 1 will never be executed at the same time as block 2.

The following code defines two synchronized blocks. But they are associated with two different locks, one is the class object, and the other is the instance object. Those two synchronized blocks will never wait for each other.

class class_name {
   type method_name() {
      synchronized (this) {
         statement block 1
      }
   }
   static synchronized type method_name() {
      statement block 2
   }
}

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

Synchronization Technique and Synchronized Code Blocks

 Why Synchronization Is Needed in Multi-Threading Applications?

 Synchronization Technique - Lock and Synchronized Code

"synchronized" - How Java Supports Synchronization

 BankingThread.java - Synchronization Sample Program

 BankingThread.java - Synchronization Sample Program Output

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version

"synchronized" - How Java Supports Synchronization - Updated in 2014, by Dr. Herong Yang