[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[jfriends-ml 12540] java のプログラムを Ctrl+C で止める
- From: "IWAMURO Motonori" <deenheart+jfriends@xxxxxxxxx>
- Date: Sat, 11 Nov 2006 22:29:58 +0900
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:sender:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; b=cTB7XthqqpVeR6FTDXXKNdO+KJjtzuSgRbjYmw2v9nt2qbXghQClxLNvzxaqE0lwI0CMqM3pk07S0yY+wOR6OYWS56IK7nN6QJYWhhrimTCUlmf7GayQzUIYoCOX7tC4Qe3+0DaPEEfhSz/ps3tg0LM+XgFZUvOLfOKZe55X+Og=
岩室です。
飲み会の雑談で出たネタ。
javaのプログラムをCtrl+Cで止めたとき、安全に終了します。
元ネタはココ(↓)。
http://www.mailpia.jp/mpblog/tfukui/cat24/java/
public class Test {
public static void main(String[] args) {
final Thread main = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Starting shutdown...");
System.out.flush();
main.interrupt();
try {
main.join();
} catch (InterruptedException e) {
// no operation
}
System.out.println("Done.");
System.out.flush();
}
});
int i = 0;
while (!Thread.interrupted()) {
System.out.println(++i);
System.out.flush();
}
System.out.println("Interrupted!!");
System.out.flush();
}
}
--
IWAMURO Motnori