Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Finally not executed after try block  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23505
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23505
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23505
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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?
 
Sheriff
Posts: 23506
47
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!