Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Threads

 
Front Enista
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Front Enista
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Front Enista
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, now it's clear.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry Wong for clearing my misconception
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic