• 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

Finally not executed after try block

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to run the below concurrency code:



and the output is :



According to the output it seems finally was not executed despite the try block getting completed. Am I right with the understanding? And why was the exception immediately thrown before going to the finally block?
 
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

saurabhmehta mehta wrote:
According to the output it seems finally was not executed despite the try block getting completed. Am I right with the understanding? And why was the exception immediately thrown before going to the finally block?



Not sure how you draw that conclusion since there are messages in the output that can only be produced by the finally block...

Henry
 
saurabhmehta mehta
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

saurabhmehta mehta wrote:
According to the output it seems finally was not executed despite the try block getting completed. Am I right with the understanding? And why was the exception immediately thrown before going to the finally block?



Not sure how you draw that conclusion since there are messages in the output that can only be produced by the finally block...

Henry



Yes I can see that, but after exiting the try block the control first goes to the statements outside the finally block and then to the statements inside the finally are executed. Is that the normal execution flow. Please help if I am missing something..
 
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

saurabhmehta mehta wrote:
Yes I can see that, but after exiting the try block the control first goes to the statements outside the finally block and then to the statements inside the finally are executed. Is that the normal execution flow. Please help if I am missing something..



Keep in mind that you have more than one thread here. You need to follow the flow of each thread independently.

Also, you seem to be mixing standard out with standard error, so, it is possible for you messages to be out of order too.

Henry
 
saurabhmehta mehta
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Also, you seem to be mixing standard out with standard error, so, it is possible for you messages to be out of order too.

Henry



There are 2 threads in the program, the new thread executes statements inside the run method whereas the main thread runs inside the main method, is my understanding correct? From the output it seems the main thread after the line " executor.awaitTermination(5, TimeUnit.SECONDS);" skipped the finally block and directly executed the statements outside it. Then it came back inside the finally method. All this is done by the main thread.Is this a normal flow for a single thread?
 
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

saurabhmehta mehta wrote:
There are 2 threads in the program, the new thread executes statements inside the run method whereas the main thread runs inside the main method, is my understanding correct? From the output it seems the main thread after the line " executor.awaitTermination(5, TimeUnit.SECONDS);" skipped the finally block and directly executed the statements outside it. Then it came back inside the finally method. All this is done by the main thread.Is this a normal flow for a single thread?



How did you draw that conclusion? That line doesn't have any output.  And also, how do you know the main thread "executed statements out of it"? Heck, what does that even mean?  

Are you referring to the "running task" output? This output is printed by the other thread -- not the main thread.

Henry
 
saurabhmehta mehta
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Are you referring to the "running task" output? This output is printed by the other thread -- not the main thread.

Henry



No I am not.

Henry Wong wrote:

How did you draw that conclusion? That line doesn't have any output.  And also, how do you know the main thread "executed statements out of it"? Heck, what does that even mean?  

Henry



Lets go by the line numbers. According to the output, lines 22 and 23 were executed and then the lines 19 and 20. My question is when the control left the try block at line 14 shouldn't it have first executed the finally block first since normally finally block has to be executed once the try/catch is complete? Why is the control going back to the line that should have been executed first?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saurabhmehta mehta wrote:Lets go by the line numbers. According to the output, lines 22 and 23 were executed and then the lines 19 and 20.



No, we don't know that from the output. The two lines which produce output use different output streams, and buffering could result in them writing output to the console in a different order than what they were run in.

So let's get rid of the System.out/System.err source of confusion and see what happens then.
 
reply
    Bookmark Topic Watch Topic
  • New Topic