gzz
gzz

Reputation: 675

How to crash a thread or intentionally hung the thread?

Just curious on how I can check a thread status after crashes. so far, i did some System.exit(0) or (1) but it seemed to me the thread is still alive and runnable - was expecting for it to be terminated. Here's my test code on checking the thread

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());

}

and inside the runnable class, I intendedly use System.exit(1) or (0). I too did make it throw an Error but still showing RUNNABLE state of the thread.

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

}

Below is the console output

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

I hope the info above is sufficient, thanks for advices.

Upvotes: 3

Views: 1381

Answers (4)

Masked Man
Masked Man

Reputation: 2538

As a combination of Philip Voronov and Geek answers: The code you're looking for is something like this:

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;
    }
}

and here is the output I got:

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

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533482

Threads don't start instantly (in fact nothing happen instantly in Java)

When you check the status of the thread it probably hasn't actually start and it hasn't called System.exit(1). If it had you wouldn't get the output as it would have killed the whole process.

Instead of thinking about get a result of a Thread I suggest submitting tasks to an ExecutorService. e.g.

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

String result = future.get();

An even simpler way to submit multiple jobs to a thread pool and collect the results is to use parallelStream

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

Upvotes: 0

Filipp Voronov
Filipp Voronov

Reputation: 4197

System.exit() doesn't kill a thread, it kills your application (it's a syscall, it deals with application as a whole, not internal java call at java threads level).


In your case it seems that thread's System.exit() executes after your second check on the thread (remember it runs in parallel).

Upvotes: 0

Geek
Geek

Reputation: 23409

The thread is actually alive when the last two Sysouts of the main are run. You need to put a sleep in the Main thread. May be 5 seconds.

Upvotes: 1

Related Questions