How long a thread will be alive in java?

2019-08-12 03:01发布

问题:

I create a thread using

Thread t = new Thread();
t.start();

You start a thread using t.start(); Now how long the thread will be alive? To what state it will go after X (the answer of above question) seconds?

Thread t = new Thread();
t.start();
public void run(){
    System.out.println("Threads");
}

What will happen if the thread has run() method?

回答1:

A thread created and started exactly as you describe will be alive only for as long as the empty Thread.run() method takes to do nothing and return. When the thread terminates, the t.isAlive() function will return false.

Normally, a thread does something useful and will be alive for as long as the run() method has not returned.