有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程这个Java线程什么时候停止

我只是想知道当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;
        }
    }
}

共 (0) 个答案