简体   繁体   English

Java中的一个线程将存活多长时间?

[英]How long a thread will be alive in java?

I create a thread using 我使用创建一个线程

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

You start a thread using t.start(); 您使用t.start()启动线程; Now how long the thread will be alive? 现在线程将存活多长时间? To what state it will go after X (the answer of above question) seconds? X(上述问题的答案)秒后它将进入什么状态?

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

What will happen if the thread has run() method? 如果线程具有run()方法,将会发生什么?

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. 只要空的Thread.run()方法不执行任何操作并返回,按照您所描述的完全创建和启动的线程将一直存在。 When the thread terminates, the t.isAlive() function will return false . 当线程终止时, t.isAlive()函数将返回false

Normally, a thread does something useful and will be alive for as long as the run() method has not returned. 通常,线程会做一些有用的事情,只要run()方法没有返回,线程就可以存活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM