2012-01-09 1 views
2

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Timer.htmlTimer.cancel()が呼び出されたときにjava.util.Timerによって起動されたスレッドの実行の動作は何ですか?

この質問の理由は、私は時々解決するために5秒を要し、その後、ディスク上のファイルに書き込み、ネットワーク要求を行う1分間隔で実行されているタイマーを持っているということです。

アプリケーションが終了したら、ファイルハンドラをクリーンアップする必要がありますが、タイマーによって開始されたスレッドが現在ネットワークコールでブロックされていて、閉じたファイルハンドラ

Timer.stop()が任意の時点で呼び出されたときに何が起きるかを決定的にテストするのは難しいので、Javaで作業する人や同様の問題を解決した人が助けてくれることを期待しています。

+0

問題の範囲を明確にするために、私は同じことをやっている複数のタイマーを持っているかもしれません。これらはすべてこの問題に対処する必要があります。 – merlin2011

+1

'#stop()'ではなく 'java.util.Timer#cancel()'を意味しますか? – Ramon

答えて

2

あなたのアップデートからは、それが何をするのかを説明するTimer.cancelという意味に見えます。私はあなたがjava.util.Timerとしてjavax.swing.Timerがstop()メソッド、およびjavax.management.timer.Timersun.misc.Timer持つ(持っているストップ()メソッドをではなく、あいまいされている)しない意味を前提と

/** 
* Terminates this timer, discarding any currently scheduled tasks. 
* Does not interfere with a currently executing task (if it exists). 
* Once a timer has been terminated, its execution thread terminates 
* gracefully, and no more tasks may be scheduled on it. 
* 
* <p>Note that calling this method from within the run method of a 
* timer task that was invoked by this timer absolutely guarantees that 
* the ongoing task execution is the last task execution that will ever 
* be performed by this timer. 
* 
* <p>This method may be called repeatedly; the second and subsequent 
* calls have no effect. 
*/ 
public void cancel() { 
    synchronized(queue) { 
     thread.newTasksMayBeScheduled = false; 
     queue.clear(); 
     queue.notify(); // In case queue was already empty. 
    } 
} 

私の知る限り、そのAこのタイマーをIO用に使用することは非常に悪い考えです。これはGUIスレッドを使用し、一定期間ロックアップする可能性があるからです。

あなたがものを実行している)、それは新しいタスクの送信を停止見ることができますが、(中断または停止しないコード

/** 
* Stops the <code>Timer</code>, 
* causing it to stop sending action events 
* to its listeners. 
* 
* @see #start 
*/ 
public void stop() { 
    getLock().lock(); 
    try { 
     cancelEvent(); 
     timerQueue().removeTimer(this); 
    } finally { 
     getLock().unlock(); 
    } 
} 

を見てみます。

+0

私の改訂版をご覧ください。私は質問を書いたときに間違ったタイマーのドキュメントを参照していましたが、実際に使用しているタイマーはjava.utilのものです。 – merlin2011

+0

私のアプリケーションはjava.util.Timerを使ってコンソールアプリケーションです。 – merlin2011

+0

私がjava.util.Timerに 'stop()'メソッドがないと指摘したので、あなたが意味することを明確にすることはできますか? –

0

デーモンモードでタイマーを使用していますか?デーモンスレッドとは何かを示すこの文書を参照してください。

/** 
* Creates a new timer whose associated thread may be specified to 
* {@linkplain Thread#setDaemon run as a daemon}. 
* A daemon thread is called for if the timer will be used to 
* schedule repeating "maintenance activities", which must be 
* performed as long as the application is running, but should not 
* prolong the lifetime of the application. 
* 
* @param isDaemon true if the associated thread should run as a daemon. 
*/ 
public Timer(boolean isDaemon) { 
    this("Timer-" + serialNumber(), isDaemon); 
} 

あなたがデーモンスレッドを使用している場合、アプリケーションはこのスレッドで保留中の操作は最後まで保証されませんので、完了するために、このスレッドを待ちません。ただし、デーモンスレッドでない場合は、このスレッドが最後の操作を完了した後にのみ、アプリケーションはシャットダウンします。

ファイルハンドルの終了時に、タイマースレッド自体でそれを行うのはなぜですか?それができない場合、Shudownフックは、アプリケーションのシャットダウン時に何かを行うための理想的な方法です。

したがって、タスクの実行ごとにタイマーによって取得できる共通のロックを持つことができ、開いているファイルハンドルを閉じるためにシャットダウンタスクによって同じロックを取得できます。

私は事を明確にすることを望む。

関連する問題