I am new in Java and I have found one interesting thing. Now I'm learning sockets and while I was debugging my program I noted that there are several Threads which I didn't create. Then I put a breakpoint at the very beginning of my program and when I opened threads I can see all threads that are the part of Socket. But I didn't create it yet, because it was FIRST line of code. I want to now where those threads come from and why they are already created if socket is still not created.
            Asked
            
        
        
            Active
            
        
            Viewed 1,009 times
        
    0
            
            
        - 
                    At least some of those threads are discussed at https://stackoverflow.com/questions/5766026/default-threads-like-destroyjavavm-reference-handler-signal-dispatcher, https://stackoverflow.com/questions/235674/what-is-the-java-signal-dispatcher-thread, and https://stackoverflow.com/questions/40552289/what-are-these-jvm-daemon-threads-created-in-simple-java-code – Slaw Dec 17 '18 at 11:18
 - 
                    Java itself creates a number of threads for garbage collection and other background maintenance. They are not in anyway related to your socket. – Mark Rotteveel Dec 17 '18 at 11:20
 
1 Answers
7
            The threads in your screenshot are as follows:
- The "main" thread is the thread created to run your 
mainmethod. - The "Attach Listener" thread is created by the JVM to accept connections to the JVM's debug agent.
 - The "Common Cleaner" thread is related to the Java 9 
Cleanermechanism which is the better way for doing tidyup on object deletion. - The "Finalizer" thread runs 
finalizemethods on unreachable objects queued by the GC. - The "Reference Handler" thread performs processing on 
Referenceobjects queued by the GC. - The "Signal Dispatcher" thread handles native signals (e.g. SIGINT, SIGHUP, etc). Apparently, these need to be handled by a dedicated (native) thread due to the way that signal-related native APIs work.
 
These threads are all created by the JVM itself.
The JVM also has one or more native GC threads, but apparently they don't show up in the listing.  I presume that is because they don't have corresponding Thread object.  (They are ... special!)
        Stephen C
        
- 698,415
 - 94
 - 811
 - 1,216
 
