Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

ExecutorService submit as lambda of Stream forEach operation  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stream's forEach(Consumer<? super T> action) takes consumer. So ideally you can't pass anything within forEach() which can return a value.
Again ExecutorService's submit() method returns a Future object. But when I pass ExectorService's submit() as lambda in forEach(), it complies and runs successfully. Can someone please explain how it is working? Please find below code example.

 
Saloon Keeper
Posts: 9710
192
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's simplify the coming discussion a bit by calling all functional interfaces that contain a method that returns void "consumers".

Higher order functions that accept consumers don't expect a return value from the consumer. Even so, they don't require that the consumer returns void. They just ignore the return value.

This is just a special case that the Java designers chose to make to make our lives easier.
 
Sheriff
Posts: 21603
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ExecutorService has three submit methods:
* submit(Runnable)
* submit(Runnable, T)
* submit(Callable)

Unlike Runnable, Callable can a) return a result, and b) throw checked exceptions. The lambdas in forEach are Callables, the one at the end is a Runnable.
 
SwapanB Biswas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan. This seems to be a special kind of case.
 
What's that smell? I think this tiny ad may have stepped in something.
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!