4

I want my main thread to be running, as I have some listener that will be listening to request/messages in another thread and I don't want my main thread to die.

Which one is better

CountDownLatch

public static void main(String[] args) throws InterruptedException {

    startListener();
    CountDownLatch latch = new CountDownLatch(1);
    latch.await();

}

Or While with sleep

public static void main(String[] args) throws InterruptedException {

    startListener();
    while (true){
        Thread.sleep(1000);
    }

}
Sujeet
  • 143
  • 2
  • 10
  • How will the main thread know when to resume/continue? – ernest_k Jan 20 '19 at 13:36
  • What does the listener do exactly? Between the two, countdown latch is better, but there might be constructs that fit even better. – daniu Jan 20 '19 at 13:37
  • @daniu : listener will be a JMS listener which will be listening the messages throughout the day, unless we stop the process. – Sujeet Jan 20 '19 at 13:38
  • @ernest_k , I don't want to process anything further in main thread. I want it to be alive, so that my listener thread will keep on listening to the messages. – Sujeet Jan 20 '19 at 13:40
  • @Sujeet I understand that It's about how the main thread will be notified that your listeners' job is done. Can you show the code of `startListeners()` or whatever code has to finish before the main thread dies? – ernest_k Jan 20 '19 at 13:42
  • `CountDownLatch.await` is a more sophisticated mechanism that would wait until you have counted down to 0, while `Thread.sleep` is rarely a good decision, particularly when it's repetitive. Both cases are bad, there is no way to stop either of them (the former requires `latch` to be passed in `startListener`, the latter needs a reference to the main thread to be able to interrupt it). – Andrew Tobilko Jan 20 '19 at 13:58
  • What about join those thread in main thread ? Refer to example in this answer: https://stackoverflow.com/a/54022338 – Eric Jan 20 '19 at 18:49

3 Answers3

4

Assuming your listener thread is a non-daemon thread you can let the main thread finish. The JVM will continue to run as long as there are non-daemon threads executing.

CountDownLatch would be a better solution if you plan to implement a graceful shutdown. You can hit the latch and let the main thread finish by executing a cleanup logic. Think how the application will shut down e.g. you get a SIGTERM singal when listener is reading a message from the queue, what then?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Thanks, Making it non-daemon works ! Is there any impact of making it non-deamon, do we need to take care of anything else ? – Sujeet Jan 20 '19 at 13:52
  • @Sujeet you can take a look at Spring's [DefaultMessageListenerContainer](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jms/listener/DefaultMessageListenerContainer.html) to see what are the challenges of reading from JMS. – Karol Dowbecki Jan 20 '19 at 13:55
  • The only difference is that the JVM will keep running if this thread is alive. – Roee Gavirel Jan 20 '19 at 13:55
2

None.

First of all, if startListener() create a new thread, the application will keep running even if the main thread finishes. That's true as long as there is at least one non-daemon thread running. (This is different from C / C++ where the application is ending with the main. which you might confuse with).

Regarding the options you presented. Both will create an endless loop (without a way to exist), which is probably not the best practice. But if you must choose one of them the latch is better since it won't consume CPU every second.

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
0

Both proposed methods would work, but I suggest to employ the main thread in synchronous processing of incoming messages. Messages then will be processed faster and with less resources (threads). See A Simple Example of Synchronous Message Receives. Listener then is not needed.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • Actually I have multiple different kind of listener and they are listening to different different topics in their own thread. So I think a single main thread listening might not be a good idea in this case – Sujeet Jan 20 '19 at 14:17