Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

ExecutorService submit as lambda of Stream forEach operation  RSS feed

 
Greenhorn
Posts: 29
  • 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: 9986
206
  • 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: 21686
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: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan. This seems to be a special kind of case.
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!