What happens if a thread opens a socket and the main program exits? I have seen sometimes that threads will run for a second or two after the main program exits, but will the socket that the thread opened up be closed when the main program exits, or when the thread finally cleans itself up an dies?
When the program (ie. JVM process) stops, all resources are released. If there is a System.exit() call made, then the JVM stops. If on the other hand, your main thread finish execution, the JVM continues to run until there are no more non-daemon threads running. So if the thread that handles the socket communication is a non-daemon thread, it will continue to run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With