This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Errata for OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide - page 369

 
Greenhorn
Posts: 3
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I found an issue in Chapter 7, page 369 when I decided to try out the code listed there.



Expected result: The program should take roughly 40 seconds to complete due to the Thread.sleep(10) with 4000 records.

Actual result: The program completes right away no matter what huge value you put in Thread.sleep() because the intermediate operations that call that method are not running.

After some head scratching, I tried replacing the count() method with different terminal operations, like forEach, and only then did I get the expected result.  After looking at the API for the count() method I found the following:

An implementation may choose to not execute the stream pipeline (either sequentially or in parallel) if it is capable of computing the count directly from the stream source.  In such cases no source elements will be traversed and no intermediate operations will be evaluated.



In this situation, there is no need to call the intermediate operations, so it doesn’t, and we don't get to see the effect of the Thread.sleep() method.  So, the count() method should be replaced by some other terminal operation so that the intermediate operations run to call the processRecord() method with Thread.sleep().
 
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look here [edit]this is the correct link (sorry for my mistake)
[/edit] and check whether I have linked to the correct book. I can't find anything for page 369, so this looks like something new.
 
Saloon Keeper
Posts: 3411
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Dave,

I'm not sure about this. It is true what you write, and I see the effect when I run the code. I did not know this. But if I look at the Java 8 documentation I am unable to find any of this documented (I do see it in the Java 10 API). So, it is probably something that was built into Java 9+. I have deleted my java 8, so can't test, but have a cow for your interesting remark.
 
Campbell Ritchie
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You deserve the cow, Piet, for noticing the different behaviour in different versions; I installed Java8u202 and it took slightly over 40″ to run.

$ java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
$ javac -version
javac 1.8.0_202
...
java WhaleDataCalculator

Tasks completed in: 40.864 seconds


 
Dave Martino
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, yes, it looks like that was something added after Java 8, so technically not errata at the time it was published.  With the authors working on the OCA/OCP 11 versions now, however, I think it's probably still something that would need to be changed for the new edition.  

Thanks for the cow!  
 
Campbell Ritchie
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree, but if the exam is geared to Java8, the original version is, as you say, correct for Java8.

Interesting question
 
author & internet detective
Posts: 39392
763
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave: It probably won't surprise you that we tested with Java 8 .

Dave/Piet: I added that to our to do list for the Java 11 book. Thanks for pointing it out. Knowing about it up front will make things faster for us.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!