我有一些(Linux)C代码,我正在转换为Java。该代码有一个主循环,用于检查每个回路顶端OS的TERM信号,否则阻塞信号。这就是每个工作单位"它在循环中完成(不会被中间的TERM信号中断)。
这已经证明有点"有趣"用Java实现。我已经提出了一些测试代码(下面),这似乎有用,但我不确定它是否会一直有效,或者我是否已经幸运了#34;在我的测试中。
那么,这就是我的问题:这个好的代码还是只是偶尔会运行的代码?
TL; DR:工作线程和关闭线程调用常见的同步方法
public class TestShutdownHook {
static int a = 0; /* should end up 0 */
static volatile int b = 0; /* exit together */
static boolean go = true; /* signaled to stop */
/*
* this simulates a process that we want to do completely
* or not at all.
*/
private static void doitall () {
System.out.println("start");
++a; /* simulates half the unit of work */
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("exception"); /* doesn't seem to happen */
}
System.out.println("end");
--a; /* the other half */
}
/*
* there can be only one
*/
private static synchronized void syncit (String msg) {
if (msg.equals("exit")) go = false;
if (go) doitall();
}
/*
* starts a thread to wait for a shutdown signal,
* then goes into the 'while go doit' loop
*/
public static void main(String[] args) throws InterruptedException {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
int n = 0;
System.out.println("Shutdown coming...");
syncit("exit"); /* can't happen while main is in syncit? */
System.out.println("Shutdown hook! " + a);
/* this isn't really needed, just lets us see "goodbye" */
while (b == 0) ++n;
System.out.println("adios..."+n);
}
});
while (go) {
syncit("loop");
// there needs to be something else in this loop
// otherwise, we will starve the shutdown thread.
// either of the two lines below seem sufficient
System.out.println("ok");
Thread.sleep(1);
}
System.out.println("goodbye");
b = 1;
}
}
答案 0 :(得分:1)
只要关闭挂钩在所有情况下运行(并完成),你应该是它,它必须没问题。根据{{3}},SIGTERM似乎按预期工作。如果您不需要使用信号处理程序明确捕获操作系统信号,我认为您不必担心。
答案 1 :(得分:1)
所有可用证据表明,如果您已注册 ShutdownHook ,那么当TERM或INT信号传递到JVM 时将启动它,并且所有其他线程将被单独存放< / strong>直到ShutdownHook的运行()方法退出(当任何仍在运行的程序将被终止时)。
因此,这会导致更简单的解决方案,避免 同步 :
public class AnotherTestShutdownHook {
static int a = 0; /* should end up 0 */
static boolean go = true; /* signaled to stop */
static Thread mainThread = Thread.currentThread();
private static void trySleep ( int msecs ) {
try { Thread.sleep(msecs); } catch (InterruptedException e) {}
}
private static void tryJoin ( Thread t ) {
try { t.join(); } catch (InterruptedException e) {}
}
/*
* this simulates a process that we want to do completely
* or not at all.
*/
private static void doitall () {
System.out.println("start");
++a;
trySleep(5000);
System.out.println("end");
--a;
}
/*
* starts a thread to wait for a shutdown signal,
* then does units of work until told to stop
*/
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Shutdown coming...");
go = false;
tryJoin(mainThread); // wait for mainThread to exit
System.out.println("Shutdown hook! " + a);
}
});
while (go) {
doitall();
}
System.out.println("goodbye");
}
}