• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Sybex CSG 17, Chapter 13

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have a question about answer E to question 25

The question goes as follows:
Assuming an implementation of the performCount() method is provided prior to runtime, which of the following are possible results of executing the following application? (Choose all that apply.)

..
E. It hangs indefinitely at runtime.
..

So in the Answers section it is said that

Finally, it is also possible for our performCount() to hang indefinitely, such as with a deadlock or infinite loop. Luckily, the call to get() includes a timeout value. While each call to Future.get() can wait up to a day for a result, it will eventually finish, so option E is incorrect.



While it's true that f.get(1, TimeUnit.DAYS) can't hang indefinetely, application itself can. If we consider that implementation for performCount() is while(true){}, call to s.shutdown() (and even s.shutdownNow()) would not stop submitted task from running, meaning the application won't terminate, as there are alive non-daemon threads.

What am I missing?
 
Master Rancher
Posts: 5175
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct - based on your description, the answer is incorrect.  With an infinite loop in performCount(), the calls to performCount() will eventually complete, the call to shutdown() will complete right away after that, and the main() method will complete.  But with all that, there will still be ten tasks running in non-daemon threads, ignoring interrupts.  The application will never complete.  

This can easily be confirmed by changing "f.get(1, TimeUnit.DAYS)" to "f.get(1, TimeUnit.SECONDS)" and running the code.  You see ten errors as each get times out... and then the code never actually completes.
 
author & internet detective
Posts: 42151
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
performCount() could hang/deadlock etc. In which case E is correct
 
Gevorg Vardanyan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replies!

Jeanne Boyarsky wrote:performCount() could hang/deadlock etc. In which case E is correct


Yes, that's exactly my point. The thing is, book says that the answer E is incorrect. See the quote in the original post
 
Greenhorn
Posts: 5
MySQL Database PHP Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, can somebody please help with this question?

I don’t understand this:

The performCount() method can also throw a runtime exception, which will then be thrown by the get() call as an ExecutionException; therefore, option D is also a correct answer.



How do we know it can throw an exception if there is no implementation provided?
Are we talking hypothesis?
Do we need to assume that methods without an implementation in the exam could throw exceptions?
Does this mean we need to think of all the possible implementations?
 
Gevorg Vardanyan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How do we know it can throw an exception if there is no implementation provided?


My understanding was that in places where implementation is omitted, we can assume any valid Java code (maybe except one containing System.exit)

In which case E is correct


So would you add this to errata, considering that infinite loop is explicitly mentioned in answer? I have spent a few hours looking into this, and wouldn't want anyone else spending time on this error
 
Jeanne Boyarsky
author & internet detective
Posts: 42151
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed it is an errata. Logging now.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic