synchronization


Thread synchronization is required when two or more threads need to share a resource. A monitor (aka semaphore) provides a mutually exclusive lock for an object. Java provides the synchronized keyword as the key that locks/unlocks an object's monitor. It can be used as a class or method modifier or as a statement (very localized). To guarantee that a variable is threadsafe (ie. not shared between threads), it can be marked as volatile. Any long running method should not be synchronized as it would become a traffic bottleneck.
Note: Each object has only one monitor. Synchronized methods may need to be contained in different classes!
Sync.java is an example of an interruptible timer that can be adapted into a splash screen if wanted. It also illustrates how wait() and notify() can be used to replace suspend() and resume().


Figure 20-1. A Java monitor.


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


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email