Question on concurrency (Scott & Jeanne's OCP 8 book)
posted 6 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi all,
I am currently preparing the Java OCP 8 exam, with Jeanne and Scott's book. In the chapter on Concurrency, review question 16 (page 400) confuses me. I am not questioning the correctness of the book. I just have troubles to wrap my head around it. The question goes as follows: What is the result of executing the following code:
The possible answers are:
A) It compiles and outputs the two numbers, followed by Printed
B) The code will not compile because of line b1
C) The code will not compile because of line b2
D) The code will not compile because of line b3
E) The code will not compile because of line b_4
F) It compiles but the output cannot be determined ahead of time
G) It compiles but throws an exception at runtime
H) It compiles but waits forever at runtime
The correct answers are G, H.
My way of thinking: The forEach() on line 4 takes a DoubleConsumer as argument. A DoubleConsumer takes one argument and returns no result (just like a regular Consumer). The argument is represented by c and that means that whatever comes after the arrow token should return void. But it was my understanding that the service() method always returns a Future object, regardless of whether it takes a Callable or a Runnable. So why doesn't this result in a compilation error? Could someone help me understand? That would be greatly appreciated.
I am currently preparing the Java OCP 8 exam, with Jeanne and Scott's book. In the chapter on Concurrency, review question 16 (page 400) confuses me. I am not questioning the correctness of the book. I just have troubles to wrap my head around it. The question goes as follows: What is the result of executing the following code:
The possible answers are:
A) It compiles and outputs the two numbers, followed by Printed
B) The code will not compile because of line b1
C) The code will not compile because of line b2
D) The code will not compile because of line b3
E) The code will not compile because of line b_4
F) It compiles but the output cannot be determined ahead of time
G) It compiles but throws an exception at runtime
H) It compiles but waits forever at runtime
The correct answers are G, H.
My way of thinking: The forEach() on line 4 takes a DoubleConsumer as argument. A DoubleConsumer takes one argument and returns no result (just like a regular Consumer). The argument is represented by c and that means that whatever comes after the arrow token should return void. But it was my understanding that the service() method always returns a Future object, regardless of whether it takes a Callable or a Runnable. So why doesn't this result in a compilation error? Could someone help me understand? That would be greatly appreciated.
Oracle Certified Professional Java SE 8
posted 6 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
I tried it on Java9 and got unpredictable output:-
The control characters are caused by my trying to run the program again; it seemed to print output but not in any particular order, and then keep running doing nothing.java Question16Demo
27.18281828459045
31.41592653589793
Printed
^[[A
^C[campbell@... ~]$ java Question16Demo
31.41592653589793
Printed
27.18281828459045
^[[A
^C[campbell@... ~]$ java Question16Demo
31.41592653589793
27.18281828459045
Printed
posted 6 years ago
-
2 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
When a function that returns void is expected, you can pass it a function that returns anything. The return value is simply ignored.
It seems to me that the correct answers should be F and H: you can't determine what the output will be because the tasks may be performed in any order and the program will keep running because the executor is never shutdown.
It seems to me that the correct answers should be F and H: you can't determine what the output will be because the tasks may be performed in any order and the program will keep running because the executor is never shutdown.
posted 6 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
@Stephan: The correct answers are indeed F and H. That was a typo from my part.
Oracle Certified Professional Java SE 8
posted 6 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Campbell and Stephan for answering.
Stephan, could you eleborate on what you mean by this? I am somewhat confused now.
Stephan van Hulst wrote:When a function that returns void is expected, you can pass it a function that returns anything. The return value is simply ignored.
Stephan, could you eleborate on what you mean by this? I am somewhat confused now.
Oracle Certified Professional Java SE 8
Campbell Ritchie
Marshal
Posts: 81610
593
posted 6 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The reason the outputs are not in any particular order is that the three Threads complete their “calculations” much faster than printing to the terminal, so they access System.out at any old time. This is a common finding when multi‑threading; the individual Threads run at different speeds and you cannot predict which will complete first.
Stephan van Hulst
Bartender
Posts: 15743
368
posted 6 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I assumed that you thought that the application wouldn't compile because you pass forEach() a function that returns a Future, while forEach() expects a function that returns void.
To that I answered that it doesn't matter, forEach() just ignores the return type of your lambda expression.
To that I answered that it doesn't matter, forEach() just ignores the return type of your lambda expression.
posted 6 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
That is indeed what is confusing me.
If I try to implement:
It should compile - but it doesn't. What am I not getting?
If I try to implement:
It should compile - but it doesn't. What am I not getting?
Oracle Certified Professional Java SE 8
Stephan van Hulst
Bartender
Posts: 15743
368
posted 6 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Right. When the return type is void, the body of the lambda expression must be a statement expression. Statement expressions are expressions that you can use as a statement, because they DO something. The following are statement expressions:
Variable assignment Variable increment/decrement Method call Object creation
An expression like a+2 or just i is not a statement and therefore can't be used as the body of a function that returns void.
An expression like a+2 or just i is not a statement and therefore can't be used as the body of a function that returns void.
posted 6 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Ok, that is something that I did not know at all, even though it seems like it is a crucial concept to grasp. I learned something new.
Thanks Stephan and Campbell for your insights! They are much appreciated.
Thanks Stephan and Campbell for your insights! They are much appreciated.
Oracle Certified Professional Java SE 8
Campbell Ritchie
Marshal
Posts: 81610
593
posted 6 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
That's a pleasureBrecht Geeraerts wrote:. . . Thanks Stephan and Campbell . . .

| Sometimes you feel like a nut. Sometimes you feel like a tiny ad. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |










