Threads ends when run finish?
Solution 1
Threads close themselves after the run method has been called. Read this for further information https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
EDIT: If you want to avoid this behaviour, I recommend using ThreadPools.
Solution 2
Yes, a thread finishes when run()
method execution ends. You can read more on threads and concurency in general here
One tip here - when using multiple threads that are started and finished all the time, it is a good idea to use a thread pool. That is because creating a thread is quite a heavy operation.
Solution 3
Threads are terminated after finishing their jobs (when the execution of run()
ends). If you want to check, use isAlive()
.
Biribu
Updated on July 07, 2022Comments
-
Biribu almost 2 years
I want to know if a thread in java closes itself when run method ends.
I mean, I have a new thread declaration:
new Thread(new SubmitDataOnBackground(handler.getIDValue(), data, this.context)).start();
And then, in SubmitDataOnBackground I have this run method:
public void run() { SubmitDataHandler submit = new SubmitDataHandler(ID, data, this.context); submit.buildAndSubmitData(); }
After buildandSubmitData finishes, does the thread close itself or I have to add any code somewhere?
I am not sure if I am leaving a new thread opened each time I call this method or it is ok.
My application is a server so it will never ends because it is active the whole time. I just want to know the amount of threads is not outnumbered because it just creates new ones without closing the others when finish.
-
Biribu almost 9 yearsOk, I am glad to know that. I want them close themselves so this is the behaviour I need. Anyway, I will read your link to know a bit more
-
Distjubo almost 9 years@Biribu if you want to call the run method a lot of times, it is recommended using ThreadPools, because they keep their threads when the run method is finished. You might want to look into that if you call it a lot
-
Stephen C almost 9 yearsThe question is not about how to stop a thread.
-
Stephen C almost 9 yearsYea ... but what does "after finishing their jobs" mean? The OP asked about the
run()
method. -
omerfarukdogan almost 9 yearsI think of run method as a Thread's job - what else could it be? :)
-
omerfarukdogan almost 9 yearsI think we are not supposed to use stop() since it is deprecated.
-
Stephen C almost 9 yearsWhat you "think" is not the point. The purpose of a SO answer is to explain to the OP in terms that he or she is likely to understand. If he asks about the
run()
method, explain it to him in terms of therun()
method. -
Stephen C almost 9 yearsThe
destroy
andstop
methods were deprecated way back in Java 1.1. You won't find any sane code using them. So it is really not worth mentioning them. Besides, that is NOT what the question is about. -
MCHAppy almost 9 years@farukdgn I have already mentioned that
stop()
is deprecated. -
MCHAppy almost 9 yearsNever mind, I am going to edit the whole answer.
-
MCHAppy almost 9 years@Biribu I have edited my answer and I have explained how Thread terminate itself with details.
-
Biribu almost 9 yearsOk, I will keep that in mind and I will try to implement it. I use the thread to save some data in my database, data gets into the server 3 or 4 times per minute so maybe it is a good idea threadpool
-
Distjubo almost 9 yearsSystem.exit() doesn't terminate the JVM abruptly. Runtime.getRuntime().halt() does.
-
MCHAppy almost 9 years@Distjubo Thank you for letting me know that. I learn something new about the JDK every day :) . See EDIT.
-
Distjubo almost 9 years@Chaker Mallek Even System.exit() always exits. But the difference is, that System.exit() will call all the shutdown hooks first.
-
MCHAppy almost 9 yearsI got it now. Thank you @Distjubo