Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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

Book Concurrency question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
below is a question from concurrency chapter in Boyarsky ocp book . The answer is C with this reasoning "as the output of submit(Runnable) task is a Future<?> object which can
only return null on its get() method."

Why can it not be a callable (service.submit(() -> counter++  as it returns a value


What is the result of executing the following program?

A. It prints 0 1 2 3 4
B. It prints 1 2 3 4 5
C. It prints null null null null null
 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, when you post a code snippet, please make sure it is formatted properly (indentation, etc). You can always check what your code will look like using the preview button.

The submit() method accept a Runnable but an overloaded version also accepts a Callable. The difference between a Runnable and a Callable is that a Callable returns something and also supports checked exception. So when you pass this lambda:
it augments the static counter variable by one but it doesn't return anything. Therefore, Java considers the lambda as a Runnable, rather than a Callable. Since a Runnable does not return anything, the Future<?> object does not contain any result. A Future object represents the result of an asynchronous computation. Calling get() on such an object will return null.
 
Brecht Geeraerts
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, in follow-up of the OP's post, I was playing with the code in my IDE and noticed something that I did not capture before.

If the lambda that is passed to the submit() method is (together with the rest of the code of course):
my IDE outputs 0 1 2 3 4

When the lambda is written like this:
my IDE outputs null null null null null

Why does wrapping the body of the lambda in braces (and adding a semi-colon) change the result?
 
Bartender
Posts: 3517
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Brecht
Why do you think that counter++ does not return anything?
Say:

Secondly: if you want to retuen some result of a lambda, you either say: -> 3; or -> {return 5;}; if you do: -> {...} without a return, then nothing is returned.

With respect to Callables and Runnables, can you explain the difference in your outcomes?
 
Brecht Geeraerts
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet. Thanks for your post. There are several things that confuse me now...    I will try to follow your steps of reasoning because I know it will help me.

Firstly: The run() method of Runnable returns void. So when passing a Runnable to the submit() it is legal in this case to omit the return keyword from the lambda body. Or am I mistaken here?

Secondly:
When passing a Runnable object to the submit method, the returning Future object won't contain a value. Therefore, calling get() on it will return null.

Thirdly:
I am confused as to why
yields a different output than

I don't see why that is...
 
Piet Souris
Bartender
Posts: 3517
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Brecht,

sorry for the delay, Strictly Come Dancing got in the way.
What I meant was that:

is seen as a Callble, while

is seen as a Runnable.
But now I am confused too.

gives me an error, while

is accepted. New for me. Anyway, counter must be static, otherwise you get the error that a local variable must be effectively fiinal.

Hmm... well, let's go back to OP, before he/she get forgotten.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!