Dung (Donny) Nguyen

Senior Software Engineer

Thread Synchronization

Thread synchronization in Java is a fundamental concept used to control the access of multiple threads to shared resources. This ensures data consistency and prevents race conditions, where two or more threads modify a shared resource concurrently, leading to unpredictable results. Here’s a detailed look at thread synchronization in Java:

Key Concepts of Thread Synchronization:

  1. Critical Sections:
    • Definition: Code sections that access shared resources and must not be executed by more than one thread at a time.
    • Example: Updating a shared variable or writing to a file.
  2. Synchronized Methods:
    • Definition: Methods that are synchronized, ensuring that only one thread can execute them at a time for a particular object.
    • Syntax: Use the synchronized keyword.
    • Example:
      public class Counter {
          private int count = 0;
      
          public synchronized void increment() {
              count++;
          }
      
          public synchronized int getCount() {
              return count;
          }
      }
      
  3. Synchronized Blocks:
    • Definition: Blocks of code within methods that are synchronized, allowing finer control over synchronization by locking only specific parts of the code.
    • Syntax: Use the synchronized keyword with a specified object.
    • Example:
      public class Counter {
          private int count = 0;
          private final Object lock = new Object();
      
          public void increment() {
              synchronized (lock) {
                  count++;
              }
          }
      
          public int getCount() {
              synchronized (lock) {
                  return count;
              }
          }
      }
      
  4. Reentrant Locks:
    • Definition: A lock implementation provided by the java.util.concurrent package, offering more flexibility than synchronized methods and blocks.
    • Example:
      import java.util.concurrent.locks.Lock;
      import java.util.concurrent.locks.ReentrantLock;
      
      public class Counter {
          private int count = 0;
          private final Lock lock = new ReentrantLock();
      
          public void increment() {
              lock.lock();
              try {
                  count++;
              } finally {
                  lock.unlock();
              }
          }
      
          public int getCount() {
              lock.lock();
              try {
                  return count;
              } finally {
                  lock.unlock();
              }
          }
      }
      
  5. Wait and Notify:
    • Definition: Mechanisms to coordinate the execution of threads by allowing one thread to wait until another thread performs a particular action.
    • Methods: wait(), notify(), notifyAll().
    • Example:
      public class Message {
          private String message;
      
          public synchronized void write(String message) {
              this.message = message;
              notify();
          }
      
          public synchronized String read() throws InterruptedException {
              wait();
              return message;
          }
      }
      
  6. Volatile Keyword:
    • Definition: A keyword used to indicate that a variable’s value will be modified by different threads. Ensures visibility of changes to variables across threads.
    • Syntax: Use the volatile keyword.
    • Example:
      public class SharedData {
          private volatile boolean flag = false;
      
          public void setFlag(boolean flag) {
              this.flag = flag;
          }
      
          public boolean getFlag() {
              return flag;
          }
      }
      

Best Practices:

Conclusion:

Thread synchronization is essential for ensuring data consistency and correctness in multithreaded applications. By using synchronized methods, blocks, and higher-level concurrency constructs, you can effectively manage access to shared resources and avoid common pitfalls like race conditions and deadlocks.