Java - 如何知道线程何时等待?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1825623/
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-08-12 23:00:21  来源:igfitidea点击:

Java - How to know when thread is waiting?

javamultithreading

提问by Stefan Hendriks

Is there any neat solution of knowing when a thread has been put into waitstatus? I am putting threads to waitand I notifythem when i need it. But sometimes I want to know if a thread is currently waiting, and if so, I have to do something else.

是否有任何巧妙的解决方案可以知道线程何时被置于wait状态?当我需要它时wait,我将线程放入并放置notify它们。但有时我想知道当前是否有线程正在等待,如果是,我必须做其他事情。

I could probably set a flag myself to true/false. But I can't imagine there is a better way to do this?

我可能可以自己设置一个标志为真/假。但我无法想象有更好的方法来做到这一点?

采纳答案by Alexander Egger

The method getState()of a thread returns a Thread.Statewhich can be:

getState()线程的方法返回 aThread.State可以是:

NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING or TERMINATED

NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING 或 TERMINATED

See Thread.State.

请参阅Thread.State

回答by Konamiman

Have you looked at Thread.getState?

你看过Thread.getState吗?

回答by Laurent K

You can have all info that you want using the ThreadMXBean.

您可以使用 ThreadMXBean 获得您想要的所有信息。

Try this code:

试试这个代码:

package com.secutix.gui.seatmap;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

public class ThreadStatus {

    private static final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            buildAndLaunchThread(i);
        }

        Thread t = new Thread(){

            @Override
            public void run() {
                while(true){
                    printThreadStatus();
                    try {
                        sleep(3000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }

        };
        t.setName("detector");
        t.start();

    }

    protected static void printThreadStatus() {
        ThreadInfo[] infos = mbean.dumpAllThreads(true, true);

        for (ThreadInfo threadInfo : infos) {
            System.out.println(threadInfo.getThreadName() + " state = " + threadInfo.getThreadState());
        }

    }

    private static void buildAndLaunchThread(int i) {
        Thread t1 = new Thread(){

            @Override
            public void run() {
                while(true){
                    try {
                        sleep(3000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }

        };
        t1.setName("t" + i);
        t1.start();

    }
}