• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Possibile Error OCP stydy guide Boyarsky/Selikoff chapter 7 Concurrency review question 9  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
I have problem understanding the following question along with the code related to the question.
For better understanding I will rewrite the whole question from the book:

Question:
Assuming MyTask is an abstract class that extends ForkJoinTask abstract class, what statements about the following code are true(Chose all that apply):



A. The code correctly finds the minimum value in the Array
B.MyTask inherits RecursiveAction
C.MyTask inherits RecursiveTask
D. The code produces an infinite loop at runtime
E. The class producess single Threaded performance at runtime
F. The code does not compile

Correct answers according to the book are A, C and E.

This is what I dont understand about this question and the code:

1. MyTask inherits from ForkJoinTask and it is on the same level as RecursiveTask so it cannot inherit from RecursiveTask.  Than why answer C is correct?
2. ForkJoinTask contains the methods fork(), join()... BUT DOES NOT contain the abstract method compute(). So FindMin does not inherit this method but still it defines a method named compute(). This method is not overridden method it is just a method that belongs to FindMin and has the same name like the abstract method in RecursiveTask. Is this correct???
3. The code like this DOES NOT COMPILE! In order to compile we need to provide implementation for the abstract methods in ForkJoinTask like exec(), getRawResult() and setRawResult(V value).
So, is there something wrong with this code or I am missing something???

Thanks in advance!



 
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the documentation for those superclasses?
 
Filip Telente
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The only thing relative to my question was in the documentation for ForkJoinTask:

The ForkJoinTask class is not usually directly subclassed. Instead, you subclass one of the abstract classes that support a particular style of fork/join processing, typically RecursiveAction for most computations that do not return results, RecursiveTask for those that do, and CountedCompleter for those in which completed actions trigger other actions. Normally, a concrete ForkJoinTask subclass declares fields comprising its parameters, established in a constructor, and then defines a compute method that somehow uses the control methods supplied by this base class.  



Why one of the answers states that the class MyTask inherits from RecursiveTask. It is true that the class defines a method compute that returns a value just like the abstract method in RecursiveTask but that doesn't mean it inherits from it. Obviously it inherits from ForkJoinTask which is why the code does not compile since it does not provide implementation for the abstract  methods there. Still the book claims that the code compiles without issue.

 
Saloon Keeper
Posts: 3252
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Filip,

I agree with you: the code should not compile. Maybe the authors meant that MyTask inherits from RecuriveTask? Have you looked at the errata, to see if this case is mentioned?

Of course, it is possible that MyTask implements all the abstract methods from FYT, but that is not given.
 
Filip Telente
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet
I checked the errata more times and there are some mistakes regarding the same question but nothing about the issues that I am mentioning here. As I rewrote the question in my first post I applied all the corrections that are mentioned in the errata. So the copy of the question in my first post is a little bit modified all according to the errata regarding the question.

I don't understand what you mean "Maybe the authors meant that MyTask inherits from RecuriveTask".
They do mean that but to me it is unclear why since it is clearly stated that MyTask extends ForkJoinTask and not RecursiveTask.

Also one issue that I forgot to mention so far is
- Why the main method in the code announce InterruptedException and ExecutionException?
In my version of the question this is not visible because I noticed that they are not needed here since none of the methods used in the main method throws that kind of exceptions, but in the original question they announced.
 
Piet Souris
Saloon Keeper
Posts: 3252
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still puzzled...

If I have this MyTask

Then the code compiles fine, but returns the wrong answer (3, the result of getRawResult).
Since the class is abstract, I can leave out that getRawResult method, but then I get an error in the FindMin class, that the gerRawResult method is not implemented. Only if I implement this metod in the FindMin class, like

do I get a correct answer, but the given code in the book does not have this method implemented. Beats me for now.

I also don't know why the book declares to throw these exceptions, since the code compiles without that. But I must obviously rehearse my knowledge about Exceptions, since I would expect the compiler to throw an error if Exceptions are declared that are never thrown... but that'll be tomorrow.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!