java java线程的isAlive()方法工作不正常?

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

isAlive() method of java thread is not working properly?

javamultithreading

提问by Rise

I was trying a example of isAlive()method of java threading. But i found that isAlive()method is returning falseeven if thread has been already started. Can someone please tell me what am i doing wrong? Here is the code snippet.

我正在尝试一个isAlive()java线程方法的例子。但是我发现即使线程已经启动,该isAlive()方法也会返回false。有人可以告诉我我做错了什么吗?这是代码片段。

package app;

public class ThreadAliveDemo {

    public static void main(String[] args) {

        Thread myThread;

        myThread = new Thread()
        {
            public void run()
            {
                            Thread.sleep(3000);
                System.out.println("My Thread.");
            }
        };

        myThread.setName("My Thread");
        myThread.start();

        if(!myThread.isAlive())
        {
            myThread.setName("My Thread");
            myThread.start();
        }

    }

}

采纳答案by Morfildur

If my memory serves me well java has quite long periods between thread switching so it is possible that the isAlive fails because the thread is not yetalive. Try to add some waiting time between thread.start() and thread.isAlive()

如果我没记错的话,java 在线程切换之间有很长的时间,所以 isAlive 可能会失败,因为线程还没有活动。尝试在 thread.start() 和 thread.isAlive() 之间添加一些等待时间

回答by skaffman

There's a good chance the thread will have started, executed, and finished, between your call to start()and your call to isAlive().

在您对 的调用start()和对 的调用之间,线程很有可能已经启动、执行和完成isAlive()

Java offers no guarantees on the sequence in which these things happen. It could execute the spawned thread immediately, or it may choose to defer it until a bit later.

Java 不保证这些事情发生的顺序。它可以立即执行生成的线程,也可以选择推迟到稍后执行。

Incidentally, your code is trying to re-start the thread after it has died. This is not permitted:

顺便说一句,您的代码正在尝试在线程死后重新启动它。这是不允许的

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

多次启动一个线程是不合法的。特别是,线程一旦完成执行就可能不会重新启动。

So calling start()after checking isAlive()is never going to work.

因此start(),检查后调用isAlive()永远不会起作用。

回答by BobTurbo

I haven't done any multithreading in java yet, but it looks to me like your thread probably will have run and exited before the isAlive() check. After all, looks like your thread just prints something out and then dies.

我还没有在 Java 中进行任何多线程处理,但在我看来,您的线程可能会在 isAlive() 检查之前运行并退出。毕竟,看起来你的线程只是打印出一些东西然后死了。

回答by user207421

I don't see the point of the code you have posted. Thread.start() starts the thread: you don't need to start it twice. I don't see how your code can realistically into a situation where it has a Thread and doesn't know whether it has been started or not; anyway there are plenty of ways to code around that so it can't happen.

我没有看到您发布的代码的重点。Thread.start() 启动线程:您不需要启动它两次。我看不出您的代码如何能够真正进入具有 Thread 并且不知道它是否已启动的情况;无论如何,有很多方法可以解决这个问题,所以它不会发生。

回答by Ferran Lopez Puig

Happened to me recently, fixed it using

最近发生在我身上,使用修复它

if(yourThread.getState() == Thread.State.NEW){    
    yourThread.start();
}

instead of yourThread.isAlive();

而不是 yourThread.isAlive();