| Author |
Question about Threads
|
Front Enista
Greenhorn
Joined: Nov 04, 2010
Posts: 6
|
|
How can this code have the following output?
run:
End of method.
Exception in thread "Thread-0" java.lang.RuntimeException: Problem
run.
at javaapplication3.Test.run(Test.java:16)
at java.lang.Thread.run(Thread.java:619)
Does not the "run" output gone always before the "Exception...."???
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2925
|
|
Front Enista wrote:
How can this code have the following output?
run:
End of method.
Exception in thread "Thread-0" java.lang.RuntimeException: Problem
run.
at javaapplication3.Test.run(Test.java:16)
at java.lang.Thread.run(Thread.java:619)
Does not the "run" output gone always before the "Exception...."???
Please try executing this program multiple times. Each time you will see a different output. The reason is that JVM manages the order in which the threads are executed. So we cannot surely tell that Test thread completes before main Thread resumes.
|
Mohamed Sanaulla | My Blog
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
Front Enista wrote:
Does not the "run" output gone always before the "Exception...."???
The "run" goes to standard output. The other messages is printed by the thread cleanup code after the read has finished -- but it goes to standard error. So, depending on which I/O stream gets flush first, you may get a different order.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Front Enista
Greenhorn
Joined: Nov 04, 2010
Posts: 6
|
|
Henry Wong wrote:
Front Enista wrote:
Does not the "run" output gone always before the "Exception...."???
The "run" goes to standard output. The other messages is printed by the thread cleanup code after the read has finished -- but it goes to standard error. So, depending on which I/O stream gets flush first, you may get a different order.
Henry
So then, 3 Threads are involved in the code?
* The "main" Thread
* The "Test" Thread
* Another one that manages the Exception writing the following info to the standard output?
Exception in thread "Thread-0" java.lang.RuntimeException: Problem
at javaapplication3.Test.run(Test.java:16)
at java.lang.Thread.run(Thread.java:619)
Am I wright?
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Front Enista wrote:
So then, 3 Threads are involved in the code?
* The "main" Thread
* The "Test" Thread
* Another one that manages the Exception writing the following info to the standard output?
Exception in thread "Thread-0" java.lang.RuntimeException: Problem
at javaapplication3.Test.run(Test.java:16)
at java.lang.Thread.run(Thread.java:619)
Am I wright?
Nope, two threads. main and Thread-0. Thread and Thread of execution are two different things. The exception occurred in one thread won't affect other threads. Here, exception occurred in Thread-0.
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Front Enista
Greenhorn
Joined: Nov 04, 2010
Posts: 6
|
|
Abimaran Kugathasan wrote:
Front Enista wrote:
So then, 3 Threads are involved in the code?
* The "main" Thread
* The "Test" Thread
* Another one that manages the Exception writing the following info to the standard output?
Exception in thread "Thread-0" java.lang.RuntimeException: Problem
at javaapplication3.Test.run(Test.java:16)
at java.lang.Thread.run(Thread.java:619)
Am I wright?
Nope, two threads. main and Thread-0. Thread and Thread of execution are two different things. The exception occurred in one thread won't affect other threads. Here, exception occurred in Thread-0.
Then I don`t understand how the word "run" is write after the line "Exception in thread "Thread-0" java.lang.RuntimeException: Problem". If two lines are in the same Thread-0, how can write "Exception in thread "Thread-0" java.lang.RuntimeException: Problem " before if the "System.out.println("run."); " is executed first???
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
Front Enista wrote:
Then I don`t understand how the word "run" is write after the line "Exception in thread "Thread-0" java.lang.RuntimeException: Problem". If two lines are in the same Thread-0, how can write "Exception in thread "Thread-0" java.lang.RuntimeException: Problem " before if the "System.out.println("run."); " is executed first???
That's because this is *not* a threads issue -- it an I/O buffer issue. The "run" is printed first, it is printed to the stdio buffers. Then the exception is printed, by the same thread, and that is printed to the stderr buffers.
Sometime in the very near future, the operation system flushes the buffers. And in this case, both stdio and stderr goes to the same place -- the screen. And it seems from your output, stderr got flush first.
Henry
|
 |
Front Enista
Greenhorn
Joined: Nov 04, 2010
Posts: 6
|
|
|
Thank you, now it's clear.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2925
|
|
Thanks Henry Wong for clearing my misconception
|
 |
 |
|
|
subject: Question about Threads
|
|
|