【问题标题】:How to crash a thread or intentionally hung the thread?如何使线程崩溃或故意挂起线程?
【发布时间】:2016-08-03 07:09:51
【问题描述】:

只是好奇如何在崩溃后检查线程状态。到目前为止,我做了一些 System.exit(0) 或 (1) 但在我看来线程仍然活着并且可以运行 - 期待它被终止。这是我检查线程的测试代码

public static void main(String[] args) {
    Runnable runnableJob = new JobThatImplementsRunnableJob();
    Thread testThread  = new Thread(runnableJob);

    System.out.println("this is the testThread "+testThread.getState());
    System.out.println("thread is alive " + testThread.isAlive());
    testThread.start();

    System.out.println("this is the testThread after starting"+testThread.getState());
    System.out.println("thread is alive " + testThread.isAlive());

}

在可运行类中,我有意使用 System.exit(1) 或 (0)。我也确实让它抛出了一个错误,但仍然显示线程的 RUNNABLE 状态。

public class JobThatImplementsRunnableJob implements Runnable {
    public void run() {
        System.exit(1);
        //System.exit(0);
        //throws Error
    }

}

下面是控制台输出

this is the testThread NEW
thread is alive false
this is the testThread after startingRUNNABLE
thread is alive true

希望以上信息足够,谢谢指教。

【问题讨论】:

  • 我会在 JosThatImplementsRunnableJob 中添加类似 int i = 3/0; 的东西,而不是 System.exit(1)
  • 你有一个竞争条件阻止你做出任何结论,也就是说,你不知道也不可能知道首先会发生什么:主线程的最后一个 System.out,或者 System.out。您的单独线程的 exit()。
  • 感谢 lino 和 GPI,线程睡眠和算术 ex 可以解决问题。

标签: java multithreading runnable jvm-crash


【解决方案1】:

当 main 的最后两个 Sysouts 运行时,线程实际上是活动的。您需要在主线程中休眠。可能是 5 秒。

【讨论】:

  • 将 System.exit(1) 更改为 lino 评论的内容加上这个可以解决问题。谢谢
【解决方案2】:

作为 Philip VoronovGeek 的组合回答: 您正在寻找的代码是这样的:

public class fun {

    public static void main(String args[]) throws Exception {
        Runnable runnableJob = new JobThatImplementsRunnableJob();
        Thread testThread  = new Thread(runnableJob);

        System.out.println("this is the testThread "+ testThread.getState());
        System.out.println("thread is alive " + testThread.isAlive());
        testThread.start();
        testThread.join();
        System.out.println("this is the testThread after starting "+ testThread.getState());
        System.out.println("thread is alive " + testThread.isAlive());
    }
}

class JobThatImplementsRunnableJob implements Runnable {
    public void run() {
         return;
    }
}

这是我得到的输出:

this is the testThread NEW
thread is alive false
this is the testThread after starting TERMINATED
thread is alive false

【讨论】:

  • 我们怎样才能让他们挂起而不实际终止?
  • @jpganz18 我已经离开 Java 区很长一段时间了,但我猜你要的是守护线程。
【解决方案3】:

System.exit() 不会杀死线程,它会杀死您的应用程序(这是一个sys调用,它处理整个应用程序,而不是 Java 线程级别的内部 Java 调用)。


在您的情况下,似乎线程的 System.exit() 在您对线程进行第二次检查后执行(请记住它是并行运行的)。

【讨论】:

    【解决方案4】:

    线程不会立即启动(实际上在 Java 中没有任何事情会立即发生)

    当您检查线程的状态时,它可能还没有真正启动,也没有调用 System.exit(1)。如果有,您将无法获得输出,因为它会杀死整个过程。

    我建议将任务提交给 ExecutorService,而不是考虑获取线程的结果。例如

    Future<String> future = executorService.submit(() -> {
        return "Success";
    });
    
    String result = future.get();
    

    将多个作业提交到线程池并收集结果的更简单方法是使用 parallelStream

    List<Result> results = list.parallelStream()
                               .map(e -> process(e)) // run on all the CPUs
                               .collect(Collectors.toList());
    

    【讨论】:

      猜你喜欢
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多