Concurrency Advanced

Unsafe thread termination to cooperative cancellation

Replace Thread.stop() and ad hoc shared flags with interruption-aware task cancellation.

✕ Forced thread termination
Thread worker = new Thread(this::runTask);
worker.start();

// May stop the thread while shared state is inconsistent.
worker.stop();
✓ Future cancellation
Future<?> worker = executor.submit(this::runTask);

// Requests interruption; the task must cooperate.
worker.cancel(true);
See a problem with this code? Let us know.
🛡

Preserves invariants

Tasks stop only at code paths designed for cancellation.

🧩

Standard control

Future represents execution, completion, and cancellation together.

🔗

Composable

Interruption works with blocking queues, locks, executors, and virtual threads.

Old Approach
Thread.stop()
Modern Approach
Future.cancel(true)
Since JDK
5
Difficulty
Advanced
Unsafe thread termination to cooperative cancellation
Available

Future and ExecutorService have been available since JDK 5 (Sept 2004).

Thread.stop() can terminate code while it owns monitors or is updating shared state, leaving objects damaged. Modern cancellation is cooperative: cancel the Future with interruption, propagate InterruptedException where possible, restore interrupt status when it cannot be propagated, and design the task to reach safe cancellation points.