Understanding the names of java Thread

I ran this code for my app

Map<Thread, StackTraceElement[]> myMap = Thread.getAllStackTraces();
Log.d("threads", myMap.toString());

I got the following results

{Thread[pool-1-thread-1,5,main]=[Ljava.lang.StackTraceElement;@e21f0dc, Thread[queued-work-looper,5,main]=[Ljava.lang.StackTraceElement;@1fec5e5, Thread[FinalizerWatchdogDaemon,5,system]=[Ljava.lang.StackTraceElement;@de438ba, Thread[process reaper,10,system]=[Ljava.lang.StackTraceElement;@d51036b, Thread[GoogleApiHandler,5,main]=[Ljava.lang.StackTraceElement;@a4b3ec8, Thread[Thread-2,3,main]=[Ljava.lang.StackTraceElement;@5dadb61, Thread[HeapTaskDaemon,5,system]=[Ljava.lang.StackTraceElement;@7d7a51c, Thread[ReferenceQueueDaemon,5,system]=[Ljava.lang.StackTraceElement;@b0d2686, Thread[FinalizerDaemon,5,system]=[Ljava.lang.StackTraceElement;@7fa6b47, Thread[main,5,main]=[Ljava.lang.StackTraceElement;@2397f74}`

I think the text Thread[pool-1-thread-1,5,main] conveys very important information, but I am not quite sure what exactly it means. Could anyone please explain what does it mean?

UPDATE: For another app, I got output {Thread[Thread-7,5,main] = .. so Thread-7 seems to indicate something different from pool-1-thread-1, so I want to understand what the names of threads indicate?

Answer

UPDATE

Thread names indicate whatever the person who wrote the code that created the thread decided. There is no simple answer to that question.

However, some names seem self-explanatory, e.g. the names listed in the formatted output below. Names like:

  • main – The main thread
  • Finalizer – The thread responsible for executing finalize() methods.
  • . . .

Other names are documented. E.g. the javadoc of new Thread() says:

Allocates a new Thread object. This constructor has the same effect as Thread (null, null, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer.

So Thread-7 would appear to be the thread created by the 8th call to new Thread(...) that didn’t specify a name.

A thread name like pool-1-thread-1 would then also be an auto-generate name, for Thread #1 in Thread Pool #1.


To print the result of calling Thread.getAllStackTraces() in an easily readable format, use code like this:

Map<Thread, StackTraceElement[]> myMap = Thread.getAllStackTraces();
for (Entry<Thread, StackTraceElement[]> entry : myMap.entrySet()) {
    System.out.println(entry.getKey());
    for (StackTraceElement elem : entry.getValue())
        System.out.println("    " + elem);
}

Example Output (Java 8)

Thread[Finalizer,8,system]
    java.lang.Object.wait(Native Method)
    java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
    java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
    java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
Thread[Attach Listener,5,system]
Thread[Signal Dispatcher,9,system]
Thread[Reference Handler,10,system]
    java.lang.Object.wait(Native Method)
    java.lang.Object.wait(Object.java:502)
    java.lang.ref.Reference.tryHandlePending(Reference.java:191)
    java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
Thread[main,5,main]
    java.lang.Thread.dumpThreads(Native Method)
    java.lang.Thread.getAllStackTraces(Thread.java:1610)
    Test8.main(Test8.java:7)

Example Output (Java 15)

Thread[Finalizer,8,system]
    [email protected]/java.lang.Object.wait(Native Method)
    [email protected]/java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:155)
    [email protected]/java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:176)
    [email protected]/java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:170)
Thread[Attach Listener,5,system]
Thread[Signal Dispatcher,9,system]
Thread[Notification Thread,9,system]
Thread[Common-Cleaner,8,InnocuousThreadGroup]
    [email protected]/java.lang.Object.wait(Native Method)
    [email protected]/java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:155)
    [email protected]/jdk.internal.ref.CleanerImpl.run(CleanerImpl.java:148)
    [email protected]/java.lang.Thread.run(Thread.java:832)
    [email protected]/jdk.internal.misc.InnocuousThread.run(InnocuousThread.java:134)
Thread[main,5,main]
    [email protected]/java.lang.Thread.dumpThreads(Native Method)
    [email protected]/java.lang.Thread.getAllStackTraces(Thread.java:1649)
    app//Test.main(Test.java:9)
Thread[Reference Handler,10,system]
    [email protected]/java.lang.ref.Reference.waitForReferencePendingList(Native Method)
    [email protected]/java.lang.ref.Reference.processPendingReferences(Reference.java:241)
    [email protected]/java.lang.ref.Reference$ReferenceHandler.run(Reference.java:213)

Leave a Reply

Your email address will not be published. Required fields are marked *