Learning paths / Threads from scratch

Threads from scratch

Start, stop, wait and share: the Java basics everything else builds on.

Start: step 1

8 steps · progress is saved in this browser

  1. Starting a threadJava · Basicsstart() runs download() on a new thread, and main carries on at once. The JVM waits for worker before it exits.
  2. Stopping a threadJava · Basicsinterrupt() asks a thread to stop. A sleeping thread answers at once, because sleep() throws InterruptedException.
  3. wait() vs sleep()Java · Basicsnapper sleeps inside synchronized. It stops running but keeps the lock, so other is BLOCKED the whole time.
  4. Why count++ loses updatesJava · BasicsTwo threads each run count++ once. The result can be 1, not 2.
  5. Why a thread never sees your flagJava · Intermediatemain sets stop = true, but worker keeps spinning forever. Without volatile, Java doesn't promise worker ever sees the write.
  6. DeadlockJava · IntermediateT1 holds A and wants B. T2 holds B and wants A. Each waits for the other, forever.
  7. How a thread pool runs tasksJava · BasicsA fixed pool runs as many tasks as it has threads. The rest wait in a queue for the next free thread.
  8. Why your Java program never exits (ExecutorService)Java · Intermediatemain submits three tasks and returns without shutdown(). The tasks run, but the pool's threads wait for more work forever.

All learning paths · All scenarios