Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Thread monitoring. How to find out non daemon live threads?

I see in JConsole that I still have 2 non daemon threads but I can't find out which exactly (total number of thread is beyond 30).
Visual VM doesn't provide such information.

like image 639
Bax Avatar asked Dec 16 '11 16:12

Bax


People also ask

What are non-daemon threads in Java?

Daemon threads are low priority threads which always run in background and user threads are high priority threads which always run in foreground. User Thread or Non-Daemon are designed to do specific or complex task where as daemon threads are used to perform supporting tasks.

Which method is used to check the current thread is daemon?

boolean isDaemon(): This method is used to check that the current thread is a daemon. It returns true if the thread is Daemon. Else, it returns false.

Why JVM terminates the daemon thread if no user threads are remaining?

Why JVM terminates the daemon thread if there is no user thread? The sole purpose of the daemon thread is that it provides services to user thread for background supporting task. If there is no user thread, why should JVM keep running this thread. That is why JVM terminates the daemon thread if there is no user thread.

What is the difference between the user thread and daemon thread?

Java offers two types of threads: user threads and daemon threads. User threads are high-priority threads. The JVM will wait for any user thread to complete its task before terminating it. On the other hand, daemon threads are low-priority threads whose only role is to provide services to user threads.


2 Answers

Run:

$ jstack PID | grep tid= | grep -v daemon

This will dump all the thread of a given Java PID, filter lines with thread names and filter out non-daemon ones.

Remove -v to print daemon threads only.

like image 66
Tomasz Nurkiewicz Avatar answered Sep 28 '22 07:09

Tomasz Nurkiewicz


Take a thread dump in VisualVM. Daemon threads have the word 'daemon' next to the thread name.

"Default RequestProcessor" daemon prio=1 tid=101e58000 nid=0x124d86000 runnable [124d85000]
like image 39
Tomas Hurka Avatar answered Sep 28 '22 07:09

Tomas Hurka