简体   繁体   中英

How to Keep Listener Thread Alive

I have a class which is a listener for incoming messages and should be alive forever (So that it can listen for incoming messages) until i explicitly disconnect the connection for it. I have declared the thread as setDaemon(false) but it terminates with the calling methods termination.

Please tell me how to keep that thread alive and also please throw some light on how to implement the Spring TaskExecutor to achieve same.

Thanks in advance. it is a listener it gets notified when someone sends message... so how do i keep it running ?

The Listener Class

public class MyListnerImpl implements Listener {
    private final connectionImpl con;   

    public MyListnerImpl(ConnectionImpl con) {
    if (con.isAuthenticated() && con.isConnected()) {
        if (logger.isInfoEnabled()) {
            logger.info("Initializing XmppListner:");
        }
        this.itsCon = con;
        Thread t1 = new Thread(this);
        t1.setDaemon(false);
        t1.start();
    }
    }

    public final void listnerInterfaceMethod(final Chat chat, final Message message) {
        System.out.println("Message" + message);
    }

    public final void run() {
    itsCon.getChatManager().addChatListener(new ChatManagerListener() {
        public void chatCreated(final Chat chat, final boolean createdLocally) {
            if (!createdLocally) {
                chat.addMessageListener(itsFbml);
            }
        }
    });
    }
}

Calling class simply creates its object and thread gets started by the Listeners constructor.

I want to keep this thread created run until i interrupt it.

There are a few things you could do that would be better than hanging the initial thread forever:

Use otherThread.join() . This will cause the current thread you are running in to sleep until the other thread has finished executing. As @nanda suggests, use ExcecutorService.shutdown() to wait until a pool of threads has finished. Use otherThread.setDaemon(false) and simply let your initial thread exit. This will set your new threads as user threads. Java will not shut down until the only threads running are daemon threads.

synchronized(this) {
    while (true) {
        this.wait();
    }
}

This will make the current thread wait on the monitor of the current class until someone calls notify() , or forever.

copied from How do you hang a thread in Java in one line?

一个线程说明活着,直到run()返回(或抛出错误/异常)如果你想保持它活着,使用循环,不要返回并捕获任何错误/异常。

This is how i solved the problems that time, So this case was not of multi threading , had just a single thread which needed to run for ever, So Inserted

public final void run() {
while(true)
{
//Run Method Logic...
}
}

And instantiated it from a spring bean. I was also looking at more fancy things for this single threaded scenario like awaitTermination(); or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM