这个Java Thread什么时候停止

时间:2014-10-28 19:17:46

标签: java multithreading

我只是想知道当timer函数完成时线程是否停止?还是有什么特别的东西要阻止它?

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;
        }
    }
}

1 个答案:

答案 0 :(得分:4)

是的,根据定义,当一个线程的run()方法(或其runnable构造的run()方法)返回时,线程停止执行。