我正在努力确保我的Java应用程序采取合理的步骤来保持健壮,其中一部分涉及优雅地关闭.我正在阅读有关关闭钩子的事情,我实际上并没有在实践中如何使用它们.
那里有一个实际的例子吗?
Let's say I had a really simple application like this one below, which writes numbers to a file, 10 to a line, in batches of 100, and I want to make sure a given batch finishes if the program is interrupted. I get how to register a shutdown hook but I have no idea how to integrate that into my application. Any suggestions?
package com.example.test.concurrency;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class GracefulShutdownTest1 …Run Code Online (Sandbox Code Playgroud)