How to stop runnable in runnable?

I am trying to create a runnable that will check if you are dead (health below 1) and if you are dead then it will stop the runnable. If you don't, it will continue. But I cannot find a way to stop the runnable. Is there a way to stop the runnable in the runnable using a script?

Note that the runnable is executed through a thread:

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

      

Runnable Example:

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

      

+3


source to share


2 answers


You can break the loop, 1:

if (health < 1) {
    break;
}

      



Or you can change the while condition:

while (health > 1) {

}

      

+1


source


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

      



0


source







All Articles