0

Consider the following code:

public class ThreadT implements Runnable {

 public void run() {
 System.out.println("run.");
 throw new RuntimeException("Problem");
 }
 public static void main(String[] args) {
 Thread t = new Thread(new ThreadT());
 t.start();
 System.out.println("End of method.");
 }
}

The output I get is:

End of method.
run.
Exception in thread "Thread-0" java.lang.RuntimeException: Problem

Why the output is not like this:

run.
Exception in thread "Thread-0" java.lang.RuntimeException: Problem
End of method.
1

3 Answers 3

2

When you start a new thread (without adding any synchronization), you have no control which statement will be executed first - the next statement of the main thread or the code of the run method in the second thread.

There is no reason to expect that the run method of the new thread would be executed prior to the next statement of the main method of the main thread.

Sign up to request clarification or add additional context in comments.

Comments

2

When you start a thread, it takes time. You are also starting a thread with the sole purpose of executing independently of the main thread. This can mean it runs after your main thread has finished.


Also the error can appear before the message.

You are writing to different streams, one is System.out, the other is System.err and the order these appear on the screen is not determined. You can avoid this with

new RuntimeException("Problem").printStackTrace(System.out);

Comments

1

I believe it's because when you run a new thread beside Main, both threads run at the same time and first it prints out the "End of method", right after it runs the second outprint (which is probably few miliseconds behind.

try this:

public class thread implements Runnable {

public void run() {
System.out.println("run.");
throw new RuntimeException("Problem");
}
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new thread());
t.start();
Thread.sleep(3000);
System.out.println("End of method.");
}
}

What I did here is to put Main thread into sleep for 3 seconds and give time to Thread t to run first.

Or you can also write join() to wait the Thread to finish first. Like this:

public class thread implements Runnable {

public void run() {
System.out.println("run.");
throw new RuntimeException("Problem");
}
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new thread());
t.start();
t.join();
System.out.println("End of method.");
}
}

2 Comments

Try using join() function - I updated my answer with the code.
The join() version is smarter than the sleep() version. There are very few legitimate reasons to call sleep(). In particular, there is no synchronization bug that can be fixed by adding a sleep() call. If you think you fixed a bug by calling sleep(), what you actually did was make the bug much less likely (maybe much, much, much less likely) to happen during testing. But the bug still is there, and if and when it ever does bite, it probably will be in the field, at a customer site, long after everyone has forgotten that it existed, and very hard to find/explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.