Keeping main thread running with Thread.sleep vs CountDownLatch

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);
}

}


#java #multithreading

5 Likes10.30 GEEK