我有一個擴展線程的類,當它啓動時,它運行一個jar的Main方法。當我終止線程時,它不會終止它啓動的進程。我該如何解決這個問題?謝謝!終止一個線程運行一個罐子
下面是從我的自定義Thread類的run()方法。
@Override
public void run() {
if(parent != null) {
MessageConsole console = new MessageConsole(parent.getConsole().getTextPane());
console.redirectOut(new Color(240, 240, 240), null);
console.redirectErr(Color.RED, null);
console.setMessageLines(20);
}
if(oParent != null) {
MessageConsole console = new MessageConsole(oParent.getConsole().getTextPane());
console.redirectOut(new Color(240, 240, 240), null);
console.redirectErr(Color.RED, null);
console.setMessageLines(20);
}
try {
somejar.SomeJar.main(args);
} catch (Exception ex) {
System.err.println("Engine failed to start!\nSuggestion: Restart the application, and if the issue is not fixed reinstall the application.");
}
}
@Stephen C,同意這是最好的解決方案。如果它不是一個選項,則可以嘗試識別子線程的一種方法是在新的ThreadGroup中啓動第一個線程,默認情況下,子項將在同一組中啓動。它不可靠,但可能比沒有好。 – 2010-12-16 22:27:17
@Peter Lawrey - 線程組太不可靠。例如,如果你依賴它們,你最終可能會殺死那些JAR線程沒有「擁有」的線程。這可能比沒有任何事情更糟糕。 – 2010-12-16 23:00:16
有沒有一種方法可以通過所有的孩子來枚舉並殺死每一個?或者可能獲得每個線程的進程ID並單獨終止它們? – Jakir00 2010-12-16 23:02:55