线程在 Java 中会存活多久? [英] How long a thread will be alive in java?

查看:47
本文介绍了线程在 Java 中会存活多久?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

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

您使用 t.start() 启动一个线程;现在线程将存活多久?X(上述问题的答案)秒后它会变成什么状态?

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");
}

如果线程有 run() 方法会发生什么?

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

推荐答案

完全按照您描述的方式创建和启动的线程将仅在空 Thread.run() 方法采用时才处于活动状态什么都不做然后返回.当线程终止时,t.isAlive() 函数将返回 false.

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.

通常,线程会做一些有用的事情,并且只要 run() 方法没有返回,它就会一直存活.

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

这篇关于线程在 Java 中会存活多久?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆