자바로 타이머 테스트 중입니다.자바 타이머에 관하여
내 프로그램은 타이머를 시작한 다음 2 초 동안 기다리고 타이머를 취소하고 j 변수를 인쇄합니다.
그러나 타이머를 취소하더라도 여전히 실행됩니다. 감사.
public static Timer time;
public static int j=0;
public static void main(String[] args)
{
try {
testrun();
Thread.sleep(2000);
time.cancel();
System.out.println("timer stop");
System.out.println(j);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void testrun()
{
time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
for(int i = 0;i<100;i++){
System.out.println(i);
j++;
}
System.out.println("End timer");
}
}, 1000, 1000);
}
가능한 복제본 [Java - Timer.cancel() v/s TimerTask.cancel()] (http://stackoverflow.com/questions/21492693/java-timer-cancel-v-s-timertask-cancel) –