Keeping main thread running with Thread.sleep vs CountDownLatch

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.

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

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Java Core (2020) | Java for beginners | Brush up your Java Skills | Get Hired

In this video we will learn about all the major concepts that come under Java Core . The course is very carefully designed and is made with keeping simplicit...

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners

Java Essentials : Learn Core Java From Basic to Advance

Learn Java Programming Using Practical Assignments. Start Building Back-end Web Applications Robust Test Automation Frameworks By End Of The Course. Learn More!

Multithreading In Java Tutorial | Java Tutorial For Beginners

This tutorial on “Java Threads” will talk about one of the core concepts of Java i.e Java Threads. It will give you a complete insight into how to create, work and synchronize with multiple threads.

How Java Became The Best Career Option for IT Jobs in 2020?

Join CETPA, India's best institute for Java online training and Certification Course. Enroll now to learn Core to Advanced Java & avail 50% discount on Java Online Course with Certificate.