Java中关闭钩子的有用示例? [英] Useful example of a shutdown hook in Java?
问题描述
我正在努力确保我的Java应用程序采取合理的步骤来保持健壮,其中一部分包括优雅地关闭。我正在阅读关闭钩子,我实际上并没有在实践中如何使用它们。
I'm trying to make sure my Java application takes reasonable steps to be robust, and part of that involves shutting down gracefully. I am reading about shutdown hooks and I don't actually get how to make use of them in practice.
那里有一个实际的例子吗?
Is there a practical example out there?
假设我有一个非常简单的应用程序,如下面的这个,它将数字写入文件,10行到一行,批量为100,我想确保给定如果程序中断,批处理完成。我得到了如何注册一个关闭钩子但我不知道如何将它集成到我的应用程序中。有什么建议吗?
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 {
final private int N;
final private File f;
public GracefulShutdownTest1(File f, int N) { this.f=f; this.N = N; }
public void run()
{
PrintWriter pw = null;
try {
FileOutputStream fos = new FileOutputStream(this.f);
pw = new PrintWriter(fos);
for (int i = 0; i < N; ++i)
writeBatch(pw, i);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally
{
pw.close();
}
}
private void writeBatch(PrintWriter pw, int i) {
for (int j = 0; j < 100; ++j)
{
int k = i*100+j;
pw.write(Integer.toString(k));
if ((j+1)%10 == 0)
pw.write('\n');
else
pw.write(' ');
}
}
static public void main(String[] args)
{
if (args.length < 2)
{
System.out.println("args = [file] [N] "
+"where file = output filename, N=batch count");
}
else
{
new GracefulShutdownTest1(
new File(args[0]),
Integer.parseInt(args[1])
).run();
}
}
}
推荐答案
您可以执行以下操作:
- 让关闭挂钩将一些AtomicBoolean(或volatile boolean)keepRunning设置为false
- (可选,
.interrupt
工作线程,如果他们在某些阻塞调用中等待数据) - 通过调用
Thread.join()
writeBatch )来完成>工作线程上的方法。 - 终止程序
- Let the shutdown hook set some AtomicBoolean (or volatile boolean) "keepRunning" to false
- (Optionally,
.interrupt
the working threads if they wait for data in some blocking call) - Wait for the working threads (executing
writeBatch
in your case) to finish, by calling theThread.join()
method on the working threads. - Terminate the program
一些粗略的代码:
- 添加
静态不稳定布尔值keepRunning = true;
-
在 run()中,您更改为
for (int i = 0; i < N && keepRunning; ++i)
writeBatch(pw, i);
在 main()中添加:
final Thread mainThread = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
keepRunning = false;
mainThread.join();
}
});
这大致是我做得优雅的拒绝所有客户在终端上点击Control-C时。
That's roughly how I do a graceful "reject all clients upon hitting Control-C" in terminal.
来自文档:
当虚拟机开始其关闭序列时,它将以某种未指定的顺序启动所有已注册的关闭挂钩,并让它们同时运行。 当所有挂钩都完成后,如果启用了finalization-on-exit,它将运行所有未读取的终结器。最后,虚拟机将停止。
也就是说,一个关闭钩子使JVM保持运行,直到钩子终止(从 run() -method返回。
That is, a shutdown hook keeps the JVM running until the hook has terminated (returned from the run()-method.
这篇关于Java中关闭钩子的有用示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!