2014-09-17 1 views
0

Timer 콘솔 응용 프로그램이 있습니다. run 프로 시저를 실행 한 후에도 여전히이 응용 프로그램이 중지되지 않고 계속 대기중인 이유는 무엇입니까?java.util.Timer 사용 후 프로그램이 종료되지 않습니다.

package timer_old; 
import java.util.Timer; 
import java.util.TimerTask; 



public final class FetchMail extends TimerTask { 

    public static void main (String[] args) 
    { 
    System.out.println("starting"); 
    TimerTask fetchMail = new FetchMail(); 
    Timer timer = new Timer(); 
    timer.schedule(fetchMail, 500); 
    //timer.cancel(); 
    System.out.println("exiting"); 
    } 

    public void run() 
    { 
    System.out.println("Fetching mail..."); 
    } 


} 

출력 :

starting 
exiting 
Fetching mail... 

답변

0

실행이 끝나면 타이머를 종료하려면 timer.cancel()으로 전화해야합니다.

public final class FetchMail extends TimerTask { 
     static Timer timer=null; 
     public static void main (String[] args) 
     { 
     System.out.println("starting"); 
     TimerTask fetchMail = new FetchMail(); 
     timer= new Timer(); 
     timer.schedule(fetchMail, 3000); 

     //timer.cancel(); 
     System.out.println("exiting"); 
     } 

     public void run() 
     { 
     System.out.println("Fetching mail..."); 
     timer.cancel(); 
     } 


    } 
1

the docs에서 :

기본적으로

, 작업 실행 스레드가 데몬 스레드로 실행하지 않기 때문에, 어플리케이션이 종료 유지할 수있다 . 호출자가 타이머의 작업 실행 스레드를 빠르게 종료하려면 호출자가 타이머의 cancel 메소드를 호출해야합니다.

관련 문제