我只是想知道当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 :(得分:4)
是的,根据定义,当一个线程的run()
方法(或其runnable构造的run()
方法)返回时,线程停止执行。