Java 如何使线断裂或故意挂线?

Java 如何使线断裂或故意挂线?,java,multithreading,runnable,jvm-crash,Java,Multithreading,Runnable,Jvm Crash,我只是好奇崩溃后如何检查线程状态。到目前为止,我做了一些System.exit(0)或(1)操作,但在我看来,该线程仍然是活动的且可运行的-希望它被终止。这是我检查线程的测试代码 public static void main(String[] args) { Runnable runnableJob = new JobThatImplementsRunnableJob(); Thread testThread = new Thread(runnableJob); S

我只是好奇崩溃后如何检查线程状态。到目前为止,我做了一些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());

}
在runnable类中,我打算使用System.exit(1)或(0)。我也让它抛出一个错误,但仍然显示线程的可运行状态

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

我希望上面的信息足够了,谢谢你的建议。

当main的最后两个sysout运行时,线程实际上是活动的。你需要在主线程中设置睡眠。可能是5秒。

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



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

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

当您检查线程的状态时,它可能还没有实际启动,也没有调用System.exit(1)。如果它有,你就不会得到输出,因为它会扼杀整个过程

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

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

String result = future.get();
Future=executorService.submit(()->{
返回“成功”;
});
字符串结果=future.get();
将多个作业提交到线程池并收集结果的更简单方法是使用parallelStream

List<Result> results = list.parallelStream()
                           .map(e -> process(e)) // run on all the CPUs
                           .collect(Collectors.toList());
List results=List.parallelStream()
.map(e->process(e))//在所有CPU上运行
.collect(Collectors.toList());

作为PhilipVoronov极客的组合回答: 您要查找的代码如下所示:

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

我将添加类似于
inti=3/0的内容,而不是
System.exit(1)
josthattimplementsRunnableJob
中,您有一个竞争条件阻止您做出任何结论,即您不知道也不可能知道首先会发生什么:主线程的最后一个System.out,或sperate线程的System.exit()。感谢lino和GPI,线程睡眠和算术ex执行技巧。更改系统。退出(1)到lino所评论的位置,再加上这一个执行技巧。thankshow我们能让它们挂起而不被终止吗?@jpganz18我已经离开Java专区很长一段时间了,但我想你要求的是对线程进行后台监控。