2009-04-01 12 views

答えて

2

あなたはrunからThrowableをキャッチTimerTaskプロキシを書くことができます。

public final class TimerTaskCatcher extends TimerTask { 
    private final TimerTask orig; 
    private final Thread.UncaughtExceptionHandler handler; 
    public TimerTaskCatcher(
     TimerTask orig, 
     Thread.UncaughtExceptionHandler handler 
    } { 
     if (orig == null || handler == null) { 
      throw new NullPointerException(); 
     } 
     this.orig = orig; 
     this.handler = handler; 
    } 
    @Override public boolean cancel() { 
     return orig.cancel(); 
    } 
    @Override public void run() { 
     try { 
      orig.run(); 
     } catch (Throwable exc) { 
      handler.uncaughtException(Thread.currentThread(), exc); 
     } 
    } 
    @Override public long scheduledExecutionTime() { 
     return orig.scheduledExecutionTime(); 
    } 
} 

ところで:あなたはjava.util.concurrent代わりのTimerを使用して検討する必要があります。

0

はい、そうですね。 Threadclassにインスタンスメソッド(setUncaughtExceptionHandler)があり、そのスレッドのUncaughtExceptionHandlerが設定されています。

public void run() { 
    Thread.currentThread().setUncaughtExceptionHandler(eh); 
} 
:あなたはこのような何かを行うことができます TimerTaskのご run方法で