Learning paths / Threads from scratch
Threads from scratch
Start, stop, wait and share: the Java basics everything else builds on.
- Starting a threadJava · Basicsstart() runs download() on a new thread, and main carries on at once. The JVM waits for worker before it exits.
- Stopping a threadJava · Basicsinterrupt() asks a thread to stop. A sleeping thread answers at once, because sleep() throws InterruptedException.
- wait() vs sleep()Java · Basicsnapper sleeps inside synchronized. It stops running but keeps the lock, so other is BLOCKED the whole time.
- Why count++ loses updatesJava · BasicsTwo threads each run count++ once. The result can be 1, not 2.
- 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.
- DeadlockJava · IntermediateT1 holds A and wants B. T2 holds B and wants A. Each waits for the other, forever.
- 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.
- 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.