Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if a thread opens a socket and the main program exits? [duplicate]

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?

like image 530
yemista Avatar asked Mar 20 '23 19:03

yemista


1 Answers

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.

like image 70
anonymous Avatar answered Mar 22 '23 15:03

anonymous