If you System.exit() your application or if your machine or operating system crashes or is turned off, there is not a good way to avoid pending work to be aborted.
The best thing you can do is to record the fact that something needs to be done persistently and check for it periodically.
If you wonder specifically about thread termination, the idea is that a Java runtime terminates itself as soon as there is no non-daemon thread left. So if you make your sleep thread not a daemon, it will stop the VM from ending itself. However this mechanism is rather dubious as you can't control very well when it will end.
If this is a GUI application, you will have to check yourself if it has pending work and deny the shutdown request before calling System.exit.
Just to mention it for completeness, there are also shutdown hooks, but it’s not a good idea to do heavy lifting in them, since they can't delay shutdowns indefinitely.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…