簡體   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