0Pricing
Java Academy · Lesson

Synchronization & Locks

Use synchronized blocks and explicit locks to coordinate access to shared resources.

Synchronization & Locks is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Synchronization avoids race conditions when multiple threads access shared data.

synchronized

The synchronized keyword ensures only one thread at a time enters a block or method.

Code: synchronized method

This synchronized method prevents race conditions when incrementing a counter.

public class Main {
  private int count = 0;
  public synchronized void increment() {
    count++;
  }
  public static void main(String[] args) throws Exception {
    Main obj = new Main();
    Thread t1 = new Thread(() -> { for(int i=0;i<1000;i++) obj.increment(); });
    Thread t2 = new Thread(() -> { for(int i=0;i<1000;i++) obj.increment(); });
    t1.start(); t2.start();
    t1.join(); t2.join();
    System.out.println("Count = " + obj.count);
  }
}

synchronized block

You can synchronize only critical sections instead of whole methods, reducing contention.

Code: synchronized block

A synchronized block protects only the increment line, which is the critical section.

public class Main {
  private int count = 0;
  public void add() {
    synchronized(this) {
      count++;
    }
  }
  public static void main(String[] args) throws Exception {
    Main obj = new Main();
    Thread t1 = new Thread(() -> { for(int i=0;i<1000;i++) obj.add(); });
    Thread t2 = new Thread(() -> { for(int i=0;i<1000;i++) obj.add(); });
    t1.start(); t2.start();
    t1.join(); t2.join();
    System.out.println("Count = " + obj.count);
  }
}

Code: ReentrantLock

ReentrantLock provides explicit locking with flexibility, such as tryLock() and fairness options.

import java.util.concurrent.locks.*;public class Main {
  private int count = 0;
  private Lock lock = new ReentrantLock();
  public void increment() {
    lock.lock();
    try { count++; }
    finally { lock.unlock(); }
  }
  public static void main(String[] args) throws Exception {
    Main obj = new Main();
    Thread t1 = new Thread(() -> { for(int i=0;i<1000;i++) obj.increment(); });
    Thread t2 = new Thread(() -> { for(int i=0;i<1000;i++) obj.increment(); });
    t1.start(); t2.start();
    t1.join(); t2.join();
    System.out.println("Count = " + obj.count);
  }
}

Synchronization check

Quick check: What is the purpose of the synchronized keyword?

Recap

Recap: Use synchronized for mutual exclusion, blocks for fine-grained control, and explicit locks for advanced scenarios.

Frequently asked questions

Is the “Synchronization & Locks” lesson free?

Yes — the full text of “Synchronization & Locks” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Synchronization & Locks”?

Use synchronized blocks and explicit locks to coordinate access to shared resources. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Synchronization & Locks” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Synchronization & Locks
  2. Atomic & Concurrent Collections
  3. Common Concurrency Bugs
← Back to Java Academy