How to keep my program alive for as long a daemon thread is running?

筅森魡賤 提交于 2019-12-10 18:39:00

问题


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.

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.

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.

With this problem, what should i do.

Thanks


回答1:


You just create a thread and call th.setDaemon(true) before calling 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 thread if you want your thread to keep the JVM from exiting once the main thread completed.




回答2:


1) You need someThread.setDaemon(false) instead of 'true'. A daemon thread actualy does NOT stop java from shutting down.

From the 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.

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.

@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.

    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 InterruptedException to exit the polling task. You can also use the shutdown hook.




回答3:


The term "daemon thread" in Java is a bit misleading, as it really means "that thread is not supposed 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).

What you are possibly looking for is the Apache Commons Daemon project, which allows to create nice "system services", started through /etc/init.d/ entries and all. This works on Windows and *nix systems.



来源:https://stackoverflow.com/questions/6491798/how-to-keep-my-program-alive-for-as-long-a-daemon-thread-is-running

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!