ogarogar ogarogar - 6 months ago 23
Java Question

Nonstop working function in threads

I have class XYZ that implements Runnable with method run() . I making thread of it, then use .run() and after that I "put him to sleep" for some time. Is there any elegant way to run my function in method run() of class XYZ non-stop (do smth,sleep(),do smth,sleep... etc), until I turn off JFrame (where program shows his results). I came up with idea of loop that never ends (it works in the end) , but I guess that's not very elegant and competent solution.

Answer
private boolean shutDown;

public void run()
{
    while (!shutDown)
    {
        //Do Something
        Thread.sleep(500)
    }
}
public void shutDown()
{
    shutDown = true
}
Comments