• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mastering Lambdas Question

 
Will Myers
Ranch Hand
Posts: 395
Java jQuery Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can you explain some of the gotchas when switching to using Lambdas in Java? I understand that using a parallel stream in place of a stream for example is not always a good idea but I'm still a little hazy on why, is there anything else we need to keep in mind?
 
Campbell Ritchie
Marshal
Posts: 52590
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parallel streams vs ordinary streams isn't a gotcha, but it might be a performance handicap. If you look at the sort of code Jason Bullers posted earlier today, you can see that many of the gotchas disappear.
 
Jason Bullers
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell said, you could end up with a performance handicap if you choose to use parallel streams when the amount of work you are parallelizing is insufficient to warrant the extra overhead.

The only other "gotcha" I can think of (and that's kind of related to the what vs how idea) is that you have to be careful to solve the problem in a "functional" way when using parallel streams; that is, without introducing side effects such as mutation. If the work you are doing on each element of the stream mutates an external collection, for example, you could end up with incorrect results when you switch from a sequential stream to a parallel stream.
 
Maurice Naftalin
Author
Greenhorn
Posts: 20
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Will

On the question of parallel vs. sequential streams, Jason is certainly right that there needs to be sufficient parallelizable work to justify the overhead of splitting into parallel streams and later merging the results. It's quite a complicated question, though: you also have to take into account (amongst other things) how splittable the data source is and whether the intermediate operations are truly CPU-intensive.

Actually, the parallel/serial choice is going to be relatively uncommon in practice. More everyday gotchas arise from the design goals of the streams package, which assume that all stream programs are written so that they could be executed in parallel, even if it doesn't happen to be worthwhile to do so. The requirements this imposes are documented in the Stream package summary, under the headings "Non-interference", "Stateless behaviours", and "Side-effects".

I explain all this in the book. But it takes quite a few pages – more space than we have for answering questions on this forum

Regards
Maurice
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!