This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Machine Learning with R: Expert techniques for predictive modeling and have Brett Lantz on-line!
See this thread for details.
Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Question on concurrency (Scott & Jeanne's OCP 8 book)

 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 65827
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it on Java9 and got unpredictable output:-

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

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.
 
Saloon Keeper
Posts: 10675
228
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brecht Geeraerts
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stephan: The correct answers are indeed F and H. That was a typo from my part.
 
Brecht Geeraerts
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell and Stephan for answering.

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.
 
Campbell Ritchie
Marshal
Posts: 65827
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 10675
228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brecht Geeraerts
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is indeed what is confusing me.

If I try to implement:



It should compile - but it doesn't. What am I not getting?
 
Stephan van Hulst
Saloon Keeper
Posts: 10675
228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Brecht Geeraerts
    Master Rancher
    Posts: 260
    12
    IntelliJ IDE Spring Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Marshal
    Posts: 65827
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Brecht Geeraerts wrote:. . . Thanks Stephan and Campbell . . .

    That's a pleasure
     
    I'm full of tinier men! And a tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!