java 只要守护线程正在运行,如何让我的程序保持活动状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6491798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep my program alive for as long a daemon thread is running?
提问by M.J.
I have a requirement, that I want to start a poller once which will run foreever until the machine is restarted or the process is being killed. Now, I tried to start the poller from a main method using a shell script, but the problem is that as soon as the main method completed its execution, the poller also stoped working, as i am not using any servers to achieve so.
我有一个要求,我想启动一次轮询器,该轮询器将永远运行,直到机器重新启动或进程被终止。现在,我尝试使用 shell 脚本从 main 方法启动轮询器,但问题是只要 main 方法完成执行,轮询器也停止工作,因为我没有使用任何服务器来实现。
I heard something about daemon threads
, but I am wondering how to create a daemon thread, which will run forever, and help my poller to run also.
我听说了一些关于daemon threads
,但我想知道如何创建一个守护线程,它将永远运行,并帮助我的轮询器也运行。
UPDATE:
更新:
public class SomeThread extends Thread {
@Override
public void run() {
UnitPoller unitPoller = new UnitPoller();
unitPoller.doPolling();
}
public static void main(String[] args) {
SomeThread someThread = new SomeThread();
someThread.setDaemon(true);
someThread.start();
}
}
Above is my updated class, now whenever I execute this thread from the main method, it creates a thread but as soon as the execution of main method completes, my poller stops working, as the JVM shuts down.
上面是我更新的类,现在每当我从 main 方法执行这个线程时,它都会创建一个线程,但是只要 main 方法的执行完成,我的轮询器就会停止工作,因为 JVM 关闭。
With this problem, what should i do.
遇到这个问题,我该怎么办。
Thanks
谢谢
回答by Costi Ciudatu
You just create a thread and call th.setDaemon(true)
before calling th.start()
.
您只需创建一个线程并在调用th.setDaemon(true)
之前调用th.start()
。
Edit:
编辑:
The above answers the question "how to create a daemon thread", but (as the scope of the question has changed), a proper answer would be: don't create a daemon threadif you want your thread to keep the JVM from exiting once the main thread completed.
以上回答了“如何创建守护线程”的问题,但是(随着问题的范围发生变化),正确的答案是:如果您希望线程阻止 JVM 退出,请不要创建守护线程一旦主线程完成。
回答by Dorus
1) You need someThread.setDaemon(false)
instead of 'true'. A daemon thread actualy does NOT stop java from shutting down.
1)您需要someThread.setDaemon(false)
而不是“真实”。守护线程实际上不会阻止 java 关闭。
From the javadoc:
从javadoc:
void java.lang.Thread.setDaemon(boolean on)
Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.
This method must be called before the thread is started.
void java.lang.Thread.setDaemon(boolean on)
将此线程标记为守护线程或用户线程。当唯一运行的线程都是守护线程时,Java 虚拟机退出。
这个方法必须在线程启动之前调用。
2) I think it's not your main, but your run() method that finishes to soon. Try to put a while (true) loop around your doPolling method.
2)我认为这不是您的主要方法,而是您的 run() 方法很快就会完成。尝试在 doPolling 方法周围放置一个 while (true) 循环。
@Override
public void run() {
UnitPoller unitPoller = new UnitPoller();
while (true)
unitPoller.doPolling();
}
3) It's cleaner to call join()
inside the main then to rely on daemon thread behavior.
3)join()
在主内部调用然后依赖守护线程行为更干净。
try {
someThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
4) If you need a clean way to shut down the deamonthread. Consider implementing InterruptedExceptionto exit the polling task. You can also use the shutdown hook.
4)如果您需要一种干净的方法来关闭守护线程。考虑实施InterruptedException以退出轮询任务。您还可以使用关闭挂钩。
回答by Waldheinz
The term "daemon thread" in Java is a bit misleading, as it really means "that thread is notsupposed to keep the JVM alive". This means that the JVM will shut down as soon as the last non-daemon thread terminated (as you already stated in your question).
Java 中的术语“守护线程”有点误导,因为它的真正意思是“该线程不应该让 JVM 保持活动状态”。这意味着 JVM 将在最后一个非守护进程线程终止后立即关闭(正如您在问题中所述)。
What you are possibly looking for is the Apache Commons Daemonproject, which allows to create nice "system services", started through /etc/init.d/
entries and all. This works on Windows and *nix systems.
您可能正在寻找的是Apache Commons Daemon项目,它允许创建漂亮的“系统服务”,从/etc/init.d/
条目开始。这适用于 Windows 和 *nix 系统。