I have a Swing interface, when I click on a button a thread is created at infinity (the genus while(true)…).
My question is: when I close the main process, will the created threads be closed automatically?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Depends on if the threads you’ve started are daemon threads or not, and how you close the main process.
Here’s the relevant documentation from the standard library:
So, if you shut down the main process by
System.exitor by usingjframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)then all threads will die when you shut down.If you simply let the main thread (the thread running
public static void main) drop off the edge of the main method, then the threads will continue to run.