これは、割り込みを使用してスレッドを終了/終了しようとする試みと、Ctrl-c 終了の処理に関する 2 番目の投稿です。よくわかりませんが、これが私の最善の試みです。概念をより明確にする必要があります。可能な場合はコード例を示してください。
メイン クラスQuititと別のクラスthethingの 2 つのクラスがあります。メインクラス。
ターミナル経由でプログラムをロードする場合 (Linux の場合):
Java -jar Quitit.jar
Ctrl-c を押して閉じるときは、次のことを行う必要があると言っています。
Runtime.getRuntime().addShutdownHook()
スレッドがシャットダウン時に強制終了されるようにします。
- これはそれに対処する正しい方法ですか?
- 正常に終了できるようにメソッドを呼び出すことができないのはなぜですか?
Ctrl-C でシャットダウンせず、Thread.Interrupt() でシャットダウンしたい場合、以下のプログラムはそれを正しく使用しますか?
- Thread.Join() は、対象のスレッドが終了するまで呼び出し元のスレッドを停止してから続行するというのは正しいですか?
- Thread を拡張する代わりに、 Runnableスレッドの実装で同じRuntime.getRuntime().addShutdownHook()をどのように実装/呼び出しますか?
終了クラス:
public class Quitit extends Thread {
public Quitit(String name) {
try {
connect("sdfsd");
} catch (Exception ex) {
}
}
void connect(String portName) throws Exception {
Thread thh = new thething("blaghname");
Runtime.getRuntime().addShutdownHook(thh);
thh.start();
System.out.println("Thread Thh (thething) Started()");
while (true) {
try {
Thread.sleep(2000);
if (thh.isAlive()) {
System.out.println("Thread Thh (thething) isAlive");
if (thh.isInterrupted()) {
System.out.println("Thread Thh (thething) is Inturrupted Should be Shutting Down");
} else {
System.out.println("Thread Thh (thething) is Not Inturrupted");
thh.interrupt();
System.out.println("Thread Thh (thething) Inturrput Sent");
System.out.println("Thread Thh (thething) Joined()");
thh.join();
}
} else {
System.out.println("Thread Thh (thething) isDead");
System.out.println("Main Thread:: can now end After Sleep off 2 seconds");
Thread.sleep(2000);
System.out.println("MMain Thread:: Sleep Ended Calling Break");
break;
}
} catch (InterruptedException xa) {
System.out.println("Main Thread:: ending due to InterruptException second Break called");
break;
}
}
System.out.println("Main Thread:: Outside While(true) via Break call");
}
public static void main(String[] args) {
try {
Thread oop = new Quitit("");
Runtime.getRuntime().addShutdownHook(oop);
oop.start();
} catch (Exception ezx) {
System.out.println("Main Thread:: Not Expected Exception");
}
}
}
TheThing クラス:
public class thething extends Thread {
thething(String name) {
super(name);
}
@Override
public void run() {
while (true) {
try {
System.out.println("thething class:: Inside while(true) Loop, now sleeping for 2 seconds");
Thread.sleep(2000);
} catch (InterruptedException e) {
try {
System.out.println("thething class:: has been Inturrupted now sleeping for 2 seconds!!");
Thread.sleep(2000);
break; // Will Quit the While(true) Loop
} catch (InterruptedException ex) {
System.out.println("thething class:: Second InterruptedException called !!");
}
}
}
System.out.println("thething class:: Outside while(true) and now thread is dying");
}
}
出力::
run:
Thread Thh (thething) Started()
thething class:: Inside while(true) Loop, now sleeping for 2 seconds
Thread Thh (thething) isAlive
Thread Thh (thething) is Not Inturrupted
Thread Thh (thething) Inturrput Sent
Thread Thh (thething) Joined()
thething class:: has been Inturrupted now sleeping for 2 seconds!!
thething class:: Outside while(true) and now thread is dying
Thread Thh (thething) isDead
Main Thread:: can now end After Sleep off 2 seconds
MMain Thread:: Sleep Ended Calling Break
Main Thread:: Outside While(true) via Break call
FINISHED - BUILD SUCCESSFUL (total time: 8 seconds)