1
ファイル解析のタスクを実行するスレッドがあります。 Tomcatの起動からシャットダウンまでのバックグラウンドで実行されるデーモンスレッドとして設定されています。割り込み方法を使用したスレッド終了
私は、中断やサーバのシャットダウン時にスレッド終了を処理しようとしています。私は正しく行くかどうかを知りたい。
class LoadingModule{ // Thread is started from here
threadsStartMethod() {
Thread t = new Thread(FileParseTask);
t.setDaemon(true);
t.start();
}
}
Class FileParseTask implements Runnable {
@Override
public void run() {
try {
while(!Thread.currentThread.isInterrupted) {
// poll for file creation
// parse and store
}
} catch(Exception exit) {
log.error(message);
Thread.currentThread.interrupt();
}
}
}
これはすべてのシナリオでスレッドを正常に終了しますか?
は私がするために、catchブロックでThread.CurrentThread.interrupt()を呼び出す必要がありますか何の問題もなく、最大2Kのスレッドで私のために働いてい
を次割り込みフラグをtrueに設定しますか?またはwhile(!Thread.CurrentThread.isinInterrupted)は中断時に自動的に値をtrueにしますか? –
おそらくそうではありません。 if(instanceof Interruptedexception)ブロックの中にいるなら、ループから抜け出すのに十分な情報があると思います。 – efekctive
が必要な場合は、現在のスレッドの状態を確認することもできます。catch()からcatch()を呼び出す理由http://stackoverflow.com/questions/4906799/why-invoke-thread-currentthread-interrupt-when-catch-any-割り込みの受け取り –