How long a thread will be alive in java?

瘦欲@ 提交于 2019-12-13 00:26:40

问题


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.



来源:https://stackoverflow.com/questions/23820480/how-long-a-thread-will-be-alive-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!