We can use collarbones and future task to fetch a final result.
Runnable is not capable of returning a final result.
But Callable can hold on to the final result.
So the goal of this lesson is to use callable and future task to fetch a final value.
Have another look at the runnable interface, the run method is void, which means whatever lambda expression
that stands in for the run method will not return a value.
The lambda expression here stands in for the run method.
So if I were to remove the variable.
The value that generate no returns gets swallowed into void.
So the workaround was to basically inject a class level variable inside the expression and set it equal
to the result of generate number so that it doesn't get lost in the void.
But that's a really dirty.
I don't like this one bit.
If your task returns a result, use a callable.
The callable interface has one method named Karl, which returns a value, and this generic gives us
flexibility in the sense that if my callable is of type double, then the call method returns a double.
If Michael LaBelle is of type integer, then the call method returns an integer.
So here you have to specify the callable type.
And whatever lambda expression you put here will stand in for the kill method.
So here you have to specify a Colaba.
Followed by the collar type in this case, it's going to be a double.
Accordingly, I'll call this Colaba, and then whatever lambda expression you put here is going to stand
in for the call method.
With that being said, we do not need this anymore.
We can say callable.
And here I have to specify the collabos type in this case, double.
And you should probably call this callable now and now, whatever expression you put here is going to
stand in for the call method.
In our case, we know our callable is going to return a double value.
Let me just import callable.
OK, now, if I were to say, Hey, thread, can you run this callable?
We would get an error.
Unfortunately, threads are not designed to return a result.
You can only create a thread out of a runnable.
Well, thankfully, future task implements the runnable interface.
So by virtue of polymorphism, future task can behave as a runnable.
So as long as we wrap our callable around a future task, then create a thread out of the future task,
then we should be good.
And doing that is really easy.
All we got to say is future task.
Double.
We'll call that future is equal to a new future task.
And that's all.
And now our future task, it's going to tell me, OK, you need to wait for the task to finish and then
I'm going to grab the final results for you.
How is it going to do that?
Future task has a method for future tasks.
Get, get, wait for the task to finish and grabs the final results.
So here we can say future dagat, and once the runtime for the main thread reaches this line, FutureGen
is going to block the main thread until our task finishes.
Once the task gets fully executed, it returns the final value.
Our future gut is going to grab the final value from inside the main thread and then hear that result
gets used up.
So here we can see results is equal to future.
Don't get.
On handled exception, type, execution exception.
And for the sake of expediency, let's just catch whatever exception gets thrown at us here.
So what's going to happen is once the runtime for the main thread reaches this line, future dagat is
going to block the main thread until our task finishes.
And once the callable task gets fully executed, it retains the final value it holds on to it.
Our featured guest line is going to grab the final value from inside the main thread and then here that
result is going to get used up.
The fire and the app, while this heavy code is running in the background, I'm just interacting with
the application.
My name is Ryan.
Still running, which is fine.
And the computer returned a value of point five.
Very good.
To recap, use callable and future task if the task returns a final result as the task executes.
Future task gets the final results.