java 如何获取线程的状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5908798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:23:32  来源:igfitidea点击:

How to get the state of a thread?

javamultithreading

提问by Upvote

this is how I define my thread

这就是我定义我的线程的方式

public class Countdown implements Runnable{

    public Countdown(){
        new Thread(this).start();
    }

    //...
}

Is it still possible to get the state of a thread if it is started that way? Like

如果以这种方式启动,是否仍然可以获得线程的状态?喜欢

 Countdown cd = new Countdown();
 cd.getState();

回答by Stephen C

Is it still possible to get the state of a thread if it is started that way?

如果以这种方式启动,是否仍然可以获得线程的状态?

No. It is not.

不它不是。

If you want to be able to get the state, you have to keep a reference to the Thread; e.g.

如果你想能够获得状态,你必须保持对 Thread 的引用;例如

public class Countdown implements Runnable{
    private final Thread t;

    public Countdown(){
        t = new Thread(this);
        t.start();
    }

    public Thread.State getState() {
        return t.getState();
    }
    // ...
}


By the way, there are other reasons why this is not a great pattern:

顺便说一句,这不是一个很好的模式还有其他原因:

  • If the reference to the Countdownobject is lost (e.g. because of an exception during construction of the parent object), you will leak a Thread.

  • Threads and thread creation consume a lot of resources. If there are a lot of these Countdownobjects, or if they have a short lifetime, then you'd be better of using a thread pool.

  • 如果对Countdown对象的引用丢失(例如,由于在构造父对象期间发生异常),您将泄漏一个线程。

  • 线程和线程创建消耗大量资源。如果这些Countdown对象很多,或者它们的生命周期很短,那么最好使用线程池。

回答by Peter Lawrey

You can do

你可以做

public class Countdown implements Runnable{
    private final Thread thread;
    public Countdown(){
        (thread = new Thread(this)).start();
    }

    public Thread.State getState() {
        return thread.getState();
    }
}

回答by WhiteFang34

Since it's only implementing Runnableyou'll have to provider a wrapper method to get the state:

由于它只是实现,因此Runnable您必须提供一个包装方法来获取状态:

class Countdown implements Runnable {
    private final Thread thread;

    public Countdown() {
        thread = new Thread(this);
        thread.start();
    }

    public Thread.State getState() {
        return thread.getState();
    }
}

回答by Kaj

Sorry to say, but you should never start a thread from the constructor. That constuctor is begging for problems. Change so that the instantiator of Countdown is creating the thread.

抱歉,您永远不应该从构造函数启动线程。那个构造函数正在乞求问题。更改以便 Countdown 的实例化器创建线程。

回答by bestsss

I'd recommend using the run() method and assign the running thread there, no in the c-tor. Something along the lines.

我建议使用 run() 方法并在那里分配正在运行的线程,而不是在 c-tor 中。沿线的东西。

public class Countdown implements Runnable{
volatile Object thread = State.NEW;
public void run(){
  this.thread = Thread.currentThread();
  try{
///....
  }finally{
    this.thread = State.TERMINATED;
  }

}

State getState(){
  Object t=this.thread;
  return t instanceof State?((State)t):((Thread)t).getState();
}

}

}