• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Question on Concurrency OCP Oracle Certified Professional Java SE8 Programmer II Study Guide

 
Ranch Hand
Posts: 62
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

This question is regarding an answer on a question in one of the chapter review questions in the book: OCP Oracle Certified Professional Java SE8 Programmer II Study Guide by Jeanne Boyarsky and Scott Selikoff.

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



A. It outputs a number 10 times.
B. It outputs a Boolean value 10 times.
C. It outputs a null value 10 times.
D. It outputs Exception! 10 times.
E. It hangs indefinitely at runtime.
F. It throws an unhandled exception at runtime.
G. The code will not compile because of line o1.
H. The code will not compile because of line o2.

The answer is as follows (page 564):
21. A, C, D, E. The code compiles and runs without issue, so G and H are incorrect. The return type of performCount() is Integer, so the submit() is interpreted as being applied to a Callable<Integer> value. In this manner, the Future<?> is really a Future<Integer> object. One possible implementation of performCount() is just to return the input parameter; therefore A is a correct answer. B is incorrect, because the return type is Integer, not Boolean. The performCount() method could just return null, so C is a correct choice. The performCount() can also throw a runtime exception; therefore D is also a correct answer. It is also possible for our performCount() to hang indefinitely, such as in a deadlock. This would cause Future.get() to hang in printResults(), making E also a correct answer. Finally, any exception thrown in performCount() will appear as an exception in the get() operation. Since the get() operations are caught in a try/catch block in printResults(), none of them will be unhandled, and F is incorrect.

I have no idea how to implement the performCount() method in the case of deadlock.
Could someone show an example of implementation?

Thank you in advance.
Hiroki
 
Bartender
Posts: 5751
216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple and silly example:

give the class a static field Lock lock = new ReentrantLock();
implementation of performCount:

and have this as first line in your main:

You can save the lot by adding this line to printResults:

Add a lock.unlock() at the end of main, otherwise your program will not finish.
 
hiroki inoue
Ranch Hand
Posts: 62
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piet, thank you for reply.

It seems works.



But I expected would be modified just the performCount() method because the question mentions only will be modified the performCount() method.

Is there a way realise a deadlock just modify the performCount() method? Otherwise the answer E is not correct.

Thank you in advance.
Hiroki
 
Piet Souris
Bartender
Posts: 5751
216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Hiroki,

in the text they write: such as a deadlock, so I would not spend much time on it.

I could not think of a genuine deadlock without some additional fields. A genuine deadlock could be that performCount needs a calculator, a slideruler, an eraser, a pencil, paper, all in the form of Locks. If that method tries to get all locks, with a time lapse of a second between two tries, then all threads will be in a deadlock. That's the best I could think of.
 
a wee bit from the empire
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic