'Thread.interrupt () `cannot be used to resolve this, because it can interrupt the atom by half.
It does not. All he does is set a flag, which is almost the same as setting a mutable field.
Even if you used Thread.stop() , which is outdated and can cause any line of code to throw an error, you can still catch such an error and make sure that your data is in a consistent state.
Instead of creating queues and threads to perform tasks from this queue, I would use the ExecutorService, which has both of them, and it can be used to cancel / interrupt individual tasks, support multiple threads, exit gracefully or immediately, and wait for the termination.
Most likely, you better not redo this wheel.;)
Peter Lawrey
source share