• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

threads behaviour

 
Puspender Tanwar
Ranch Hand
Posts: 392
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output :
Exception in thread "main" runningz
java.lang.RuntimeException: main
at A.main(A.java:19)
Exception in thread "puspen" java.lang.RuntimeException: run
at A.run(A.java:9)
at java.lang.Thread.run(Unknown Source)


till now i have learnt that when an exception occurs , the program exits leaving all the code . But here, exception is occuring at //line3 first, so as per the rule, an exception has occured so the program should exit, printing the exception. But here first the "main" exception occur, then before printing the stack trace of "main" exception, it prints the "running" from the run() method. And then after printing the "main" exception stack trace, it prints the another exception which occur in run() method.
It can b clearly seen that even after the exception has occured, the another thread (here "puspen") gets executes. But why is it so ? Shouldn't the program get terminate ?
 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:
till now i have learnt that when an exception occurs , the program exits leaving all the code...

It can b clearly seen that even after the exception has occured, the another thread (here "puspen") gets executes. But why is it so ? Shouldn't the program get terminate ?


When an exception is thrown, and it is not caught, the stack will unroll, and the thread is completed.... and ... if that thread that completed is the last user thread, then the JVM will terminate.

Henry
 
Puspender Tanwar
Ranch Hand
Posts: 392
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:When an exception is thrown, and it is not caught, the stack will unroll, and the thread is completed.... and ... if that thread that completed is the last user thread, then the JVM will terminate.Henry

I was pretty sure that Henry will reply first to this. Thanks
what does it mean " stack will unroll " ?
 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:what does it mean " stack will unroll " ?


The "stack", that I am referring to in this case, is the processor call stack. And I guess "unroll" is better stated as "completely unwind" -- exiting from the first method call, and cleaning up the thread.

Henry
 
Puspender Tanwar
Ranch Hand
Posts: 392
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic