I am coding an application. It has a thread that can be requested to stop any time during its execution. Inside the thread, there are a few lines of code that should be executed all or none. In another word, if the thread runs in the middle of this block, then outside interrupt/stop request should not stop the thread before the block finishes. How can this be accomplished? This block of code has nothing to do with database write.
author and iconoclast
Depends on how the "request to stop" is being made. If you're using the deprecated stop() method, then I don't believe there's anything you can do. If you're using interrupt() or a "stop flag" of your own devising, then you simply need to not check these things during the block of statements.