• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

threads behaviour

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry
 
Straws are for suckers. Now suck on this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic