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:
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???
Have you been through the documentation for those superclasses?
posted 8 months ago
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.
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.
posted 8 months ago
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.
posted 8 months ago
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.