Threads ends when run finish?

13,205

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().

Share:
13,205
Biribu
Author by

Biribu

Updated on July 07, 2022

Comments

  • Biribu
    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
    Biribu almost 9 years
    Ok, 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
    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
    Stephen C almost 9 years
    The question is not about how to stop a thread.
  • Stephen C
    Stephen C almost 9 years
    Yea ... but what does "after finishing their jobs" mean? The OP asked about the run() method.
  • omerfarukdogan
    omerfarukdogan almost 9 years
    I think of run method as a Thread's job - what else could it be? :)
  • omerfarukdogan
    omerfarukdogan almost 9 years
    I think we are not supposed to use stop() since it is deprecated.
  • Stephen C
    Stephen C almost 9 years
    What 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 the run() method.
  • Stephen C
    Stephen C almost 9 years
    The destroy and stop 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
    MCHAppy almost 9 years
    @farukdgn I have already mentioned that stop() is deprecated.
  • MCHAppy
    MCHAppy almost 9 years
    Never mind, I am going to edit the whole answer.
  • MCHAppy
    MCHAppy almost 9 years
    @Biribu I have edited my answer and I have explained how Thread terminate itself with details.
  • Biribu
    Biribu almost 9 years
    Ok, 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
    Distjubo almost 9 years
    System.exit() doesn't terminate the JVM abruptly. Runtime.getRuntime().halt() does.
  • MCHAppy
    MCHAppy almost 9 years
    @Distjubo Thank you for letting me know that. I learn something new about the JDK every day :) . See EDIT.
  • Distjubo
    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
    MCHAppy almost 9 years
    I got it now. Thank you @Distjubo