Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中线程死亡异常的模拟_Java_Exception - Fatal编程技术网

Java中线程死亡异常的模拟

Java中线程死亡异常的模拟,java,exception,Java,Exception,我如何在java编程中模拟threaddeath异常,在什么情况下会发生这种情况,任何人都可以举个例子,抛出threaddeath异常 谢谢: public class ThreadDeathCatch { public static void main(String[] args) { try { Thread t = new Thread() { public void run() { try {

我如何在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的情况下,多线程环境中会发生线程死亡。stop

有什么想法吗


你的问题至少有一半可以通过阅读来回答。你的具体问题是什么?我需要一个直播节目来抛出threaddeathexception@anish:尝试
抛出新的ThreadDeath()
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;
}