0Pricing
Java Academy · Lesson

Common Concurrency Bugs

Identify common concurrency bugs: race conditions, deadlocks, and visibility issues.

Common Concurrency Bugs is a free Java Academy lesson on CoddyKit — lesson 3 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

Concurrency can introduce bugs: race conditions, deadlocks, starvation, and visibility problems.

Race conditions

Race conditions occur when threads interleave unpredictably, leading to wrong results.

Code: race condition

Without synchronization, increments overlap, producing wrong counts.

public class Main {
  private int count = 0;
  public 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 (expected 2000) = " + obj.count);
  }
}

Deadlock

Deadlock: two threads each hold a lock and wait for the other's lock, so both freeze.

Code: deadlock

This code may deadlock: each thread waits forever on the other's lock.

public class Main {
  private final Object lock1 = new Object();
  private final Object lock2 = new Object();
  public void method1() {
    synchronized(lock1) {
      try { Thread.sleep(50); } catch(Exception e) {}
      synchronized(lock2) { System.out.println("method1"); }
    }
  }
  public void method2() {
    synchronized(lock2) {
      try { Thread.sleep(50); } catch(Exception e) {}
      synchronized(lock1) { System.out.println("method2"); }
    }
  }
  public static void main(String[] args) {
    Main obj = new Main();
    new Thread(obj::method1).start();
    new Thread(obj::method2).start();
  }
}

Code: visibility issue

Without volatile, one thread may not see another's updates due to caching.

public class Main {
  private static boolean flag = false;
  public static void main(String[] args) throws Exception {
    Thread writer = new Thread(() -> { flag = true; });
    Thread reader = new Thread(() -> { while(!flag) {} System.out.println("Flag seen true"); });
    writer.start(); reader.start();
    writer.join(); reader.join();
  }
}

Concurrency bugs check

Quick check: Which problem occurs when two threads wait on each other's locks forever?

Recap

Recap: Bugs include race conditions, deadlocks, and visibility issues. Synchronization and volatile help avoid them.

Frequently asked questions

Is the “Common Concurrency Bugs” lesson free?

Yes — the full text of “Common Concurrency Bugs” 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 “Common Concurrency Bugs”?

Identify common concurrency bugs: race conditions, deadlocks, and visibility issues. 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 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Common Concurrency Bugs” 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