Member-only story

What are Daemon threads in Java? Why are they so crucial?

Prateek Nima
3 min readMay 20, 2024

--

Photo by Mark Fletcher-Brown on Unsplash

When working with multi-threaded applications it is extremely important to understand its entire workflow from thread creation, and thread interaction, to thread destruction. Each of the phases plays a crucial role in how your application behaves and its consistency when it is being scaled.

New threads are created for processes and the Java Virtual Machine(JVM) is not allowed to exit until all the threads running are completed even after the program execution completes. But some threads are of low priority or we can say they act as a supporting actor during the execution lifecycle of User threads. These types of threads are known as Daemon threads.

Fig(a) — Types of threads

When to utilize the daemon thread comes to user choice. Following are a few of the examples that depict the use-case of daemon threads.

Garbage collection:- JVM uses daemon thread for garbage collection by cleaning the memory and thereby allowing it to be available for reallocation.

Cache cleanup:- Clean the cache at regular time intervals. This can be considered similar to what we have with garbage collection.

--

--

Prateek Nima
Prateek Nima

No responses yet