If the return statement is executed in a thread start function the thread is stopped in java

I was confused that the thread stops automatically after the return statement is executed or it still stays alive. This is the code:

public void run{
 //code goes here
 return;//does the thread stops here;
}

      

+3


source to share


2 answers


Yes, a Java thread stops when its method ends run

.

Now this information is, say, "general knowledge", since the purpose of a class Thread

is to wrap a piece of code and exit when the code is done executing.



There is no explicit way to assert this behavior by examining the java.lang.Thread source code , since at some point the native method is called Running start0

.

+5


source


You have a thread to keep running, you need to do something like the following:



boolean stop;

public void run() {
    while(!stop) {

    }
}

      

0


source







All Articles