0
timer
함수가 완료되면 스레드가 멈추는 지 궁금합니다. 또는 그것을 멈추기 위해해야 할 특별한 것이 있습니까?이 Java 스레드가 언제 멈 춥니 다
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;
}
}
}
예 중지됩니다. 'Thread'가 멈출 때까지 기다리고 싶다면'Thread'를'join()'할 수 있습니다. –
단지 참고 사항 : 스레드의 run() 메소드를 동기화하는 좋은 이유는 거의 없습니다. –