我如何在java编程中模拟threaddeath异常,在这种情况下会出现这种情况,任何人都可以举例,抛出threaddeath异常。
感谢:
public class ThreadDeathCatch {
public static void main(String[] args) {
try {
Thread t = new Thread() {
public void run() {
try {
Thread.sleep(2500);
} catch (Throwable ex) {
ex.printStackTrace();
System.out.println("Caught in run: " + ex);
}
}
};
t.start();
// Give t time to get going...
Thread.sleep(1000);
t.stop(); // EXPECT COMPILER WARNING
} catch (Throwable t) {
System.out.println("Caught in main: " + t);
}
}
}
这是抛出Threaddeath的示例,它使用Thread.stop是否有任何其他错误的情况,在不使用Thread.stop的情况下多线程环境中可能发生线程死亡
有什么想法吗?
答案 0 :(得分:1)
System.out.println("Trying to kill current thread");
try {
Thread.currentThread().stop();
} catch (final ThreadDeath ex) {
System.out.println("Ugh I'm dead");
throw ex;
}