Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads ends when run finish? [duplicate]

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.

like image 482
Biribu Avatar asked Apr 25 '15 10:04

Biribu


1 Answers

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.

like image 133
Distjubo Avatar answered Sep 28 '22 04:09

Distjubo