繁体   English   中英

如何在runnable中停止runnable?

[英]How to stop a runnable within the runnable?

我正在尝试创建一个runnable,它将测试你是否已经死亡(健康状况低于1),如果你已经死了,那么它将阻止runnable。 如果你不是,它会坚持下去。 但我找不到阻止可运行的方法。 有没有办法用脚本停止runnable中的runnable?

请注意,runnable正在通过一个线程运行:

Thread thread1 = new Thread(runnableName);
thread1.start();

可运行的例子:

Runnable r1 = new Runnable() {
    public void run() {
        while (true) {
            if (health < 1) {
                // How do i stop the runnable?
            }
        }
    }
}

如果健康<1,你可以打破循环:

if (health < 1) {
    break;
}

或者您可以更改while条件:

while (health > 1) {

}
while (true) {
    if (health < 1) {
        // How do i stop the runnable?
        return;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM