如何在Java中检查线程是否已经启动? [英] How to check if a thread is already started or not in Java?

查看:121
本文介绍了如何在Java中检查线程是否已经启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展 Thread 的类。

我会在某个时候启动该线程。

I will start the thread at some point.

一段时间后我需要检查该线程是否已经启动?

After some time I need to check if that thread is already started or not?

这样我就可以在该特定点启动线程。

So that I can start the thread at that particular point.

我的线程类将是,

public class BasicChatListener extends Thread{

    public void run(){


    }

}

我需要知道 BasicChatListener 类的特定线程是否正在运行?
因为我的应用程序中已经运行了多个线程。

I need the know the particular thread of a BasicChatListener class is running or not ? Because I have multiple threads are already running in my application.

我们的堆栈成员将如何帮助我。

How our stack members will help me.

推荐答案

您可以使用 Thread.getState()并使用相应的状态

You can use Thread.getState() and use the corresponding states.

请注意,当您阅读此状态时,它可能已更改,但您可以确定感兴趣的内容。查看此状态图了解可用的转换。

Note that by the time you read this state it may have changed, but you may be able to determine something of interest. Check out this state diagram for the available transitions.

这篇关于如何在Java中检查线程是否已经启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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