3

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.

gzz
  • 655
  • 5
  • 18
  • Instead of `System.exit(1)`, I'd add something like `int i = 3/0;` in the `JosThatImplementsRunnableJob` – Lino Aug 03 '16 at 07:13
  • 1
    You have a race condition that prevents you from making any conclusion, that is, you do not and can not know what will happen first : the last System.out of your main thread, or the System.exit() of your sperate thread. – GPI Aug 03 '16 at 07:13
  • Thanks lino and GPI, thread sleep and arithmetic ex do the trick. – gzz Aug 03 '16 at 07:21

4 Answers4

1

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.

Geek
  • 23,089
  • 20
  • 71
  • 85
  • changing System.exit(1) to what lino commented plus this one does the trick. thanks – gzz Aug 03 '16 at 07:26
1

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
Masked Man
  • 2,176
  • 2
  • 22
  • 41
0

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).

Filipp Voronov
  • 4,077
  • 5
  • 25
  • 32
0

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());
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130