Bobby W
Bobby W

Reputation: 824

When does this Java Thread stop

I am just wondering that when the timer function finishes does the thread stop? or is there anything special I have to do to stop it?

public void testing() {
    Thread thread = new Thread(new Runnable() {
        public void run() {
            synchronized(this) {
                timer();
            }
        }
    });

    thread.start();
}

public void timer() {
    boolean active = true;

    long start = System.currentTimeMillis() / 1000L;

    while (active) {
        long finish = System.currentTimeMillis() / 1000L;

        if (finish - start >= 20) {
            System.out.println("Finished");
            active = false;
        }
    }
}

Upvotes: 0

Views: 105

Answers (1)

JB Nizet
JB Nizet

Reputation: 691685

Yes, by definition, a thread stops executing when its run() method (or the run() method of its runnable, when constructed from a Runnable) returns.

Upvotes: 4

Related Questions