Visar endast
0:03
S… Speaker 1 (4- Callables and Futures)
Sometimes we want to return a value from a task.
0:06
S… Speaker 2 (4- Callables and Futures)
For example,
0:07
S… Speaker 1 (4- Callables and Futures)
we may call the Twitter API to get the latest tweets for a given user.
0:10
S… Speaker 1 (4- Callables and Futures)
That's when we use the callable interface.
0:12
S… Speaker 1 (4- Callables and Futures)
If you look at the signature of the submit method,
0:15
S… Speaker 1 (4- Callables and Futures)
you can see that this method is overloaded.
0:18
S… Speaker 1 (4- Callables and Futures)
So far,
0:19
S… Speaker 1 (4- Callables and Futures)
we've been passing a runnable object to this method,
0:21
S… Speaker 1 (4- Callables and Futures)
but we can also pass a callable object.
0:24
S… Speaker 1 (4- Callables and Futures)
This callable is an interface very similar to the runnable interface,
0:28
S… Speaker 1 (4- Callables and Futures)
but this interface represents a task that returns a value.
0:33
S… Speaker 1 (4- Callables and Futures)
So, if you scroll down,
0:34
S… Speaker 1 (4- Callables and Futures)
you can see this method has a single abstract method called call.
0:39
S… Speaker 1 (4- Callables and Futures)
This is similar to the run method of the runnable interface,
0:42
S… Speaker 1 (4- Callables and Futures)
but instead of returning void,
0:44
S… Speaker 1 (4- Callables and Futures)
it returns v where v is the generic type parameter
0:48
S… Speaker 1 (4- Callables and Futures)
here.
0:48
S… Speaker 2 (4- Callables and Futures)
So,
0:49
S… Speaker 1 (4- Callables and Futures)
if we return a value in the body
0:53
S… Speaker 1 (4- Callables and Futures)
of this lambda, now this lambda matches a method with no parameters that
0:57
S… Speaker 1 (4- Callables and Futures)
returns a value.
0:59
S… Speaker 1 (4- Callables and Futures)
That is our call method here.
1:01
S… Speaker 1 (4- Callables and Futures)
So this lambda expression that we have passed here represents a callable object.
1:05
S… Speaker 2 (4- Callables and Futures)
Now,
1:06
S… Speaker 1 (4- Callables and Futures)
we store the result in a variable called future.
1:09
S… Speaker 1 (4- Callables and Futures)
Let's look at the type of this variable.
1:12
S… Speaker 1 (4- Callables and Futures)
That is a future of integer.
1:16
S… Speaker 1 (4- Callables and Futures)
This future is an interface that represents the future result of an operation.
1:20
S… Speaker 1 (4- Callables and Futures)
An operation that will complete in the future.
1:22
S… Speaker 1 (4- Callables and Futures)
It may complete in 500 milliseconds or 2 seconds or 5
1:26
S… Speaker 1 (4- Callables and Futures)
seconds, sometime in the future.
1:28
S… Speaker 1 (4- Callables and Futures)
So the result of this operation is not going to be calculated immediately using
1:33
S… Speaker 1 (4- Callables and Futures)
the CPU.
1:34
S… Speaker 1 (4- Callables and Futures)
So with this future object,
1:35
S… Speaker 1 (4- Callables and Futures)
we can get the future result of an operation.
1:38
S… Speaker 2 (4- Callables and Futures)
Okay?
1:38
S… Speaker 2 (4- Callables and Futures)
Now,
1:39
S… Speaker 1 (4- Callables and Futures)
to make this demo more interesting,
1:41
S… Speaker 1 (4- Callables and Futures)
let's simulate a long running operation.
1:43
S… Speaker 2 (4- Callables and Futures)
So,
1:45
S… Speaker 1 (4- Callables and Futures)
In this package,
1:46
S… Speaker 1 (4- Callables and Futures)
the executors package,
1:47
S… Speaker 1 (4- Callables and Futures)
I'm going to add a new class called longTask.
1:52
S… Speaker 1 (4- Callables and Futures)
In this class we're going to add a public static method,
1:56
S… Speaker 2 (4- Callables and Futures)
simulate,
1:58
S… Speaker 1 (4- Callables and Futures)
and here we want to put the current thread to sleep for 3 seconds.
2:02
S… Speaker 1 (4- Callables and Futures)
So thread to sleep for 3 seconds.
2:05
S… Speaker 1 (4- Callables and Futures)
Now let's handle the interrupted exception.
2:08
S… Speaker 1 (4- Callables and Futures)
Now,
2:09
S… Speaker 1 (4- Callables and Futures)
back in our demo class,
2:12
S… Speaker 1 (4- Callables and Futures)
In this task,
2:13
S… Speaker 1 (4- Callables and Futures)
instead of printing the name of the current thread,
2:15
S… Speaker 1 (4- Callables and Futures)
I'm going to call longTask .simulate.
2:19
S… Speaker 1 (4- Callables and Futures)
So we're simulating an operation that's going to take 3 seconds.
2:22
S… Speaker 1 (4- Callables and Futures)
Let's say we're going to call the tweeter API to get the latest tweets for a user.
2:26
S… Speaker 1 (4- Callables and Futures)
Eventually this task is going to return a value,
2:29
S… Speaker 1 (4- Callables and Futures)
in this case 1.
2:30
S… Speaker 2 (4- Callables and Futures)
Now,
2:31
S… Speaker 1 (4- Callables and Futures)
when we call the submit method,
2:33
S… Speaker 1 (4- Callables and Futures)
the submit method is going to return immediately.
2:35
S… Speaker 1 (4- Callables and Futures)
So it's not going to wait for 3 seconds.
2:38
S… Speaker 1 (4- Callables and Futures)
It's going to immediately return a future object.
2:41
S… Speaker 1 (4- Callables and Futures)
With this future object,
2:42
S… Speaker 1 (4- Callables and Futures)
we can get the result of this operation.
2:44
S… Speaker 1 (4- Callables and Futures)
So this object has a bunch of useful methods.
2:48
S… Speaker 1 (4- Callables and Futures)
Get,
2:49
S… Speaker 1 (4- Callables and Futures)
we can use this to get the value of this operation.
2:51
S… Speaker 1 (4- Callables and Futures)
The get is overloaded.
2:53
S… Speaker 1 (4- Callables and Futures)
With this version,
2:54
S… Speaker 1 (4- Callables and Futures)
we can pass a timeout.
2:55
S… Speaker 1 (4- Callables and Futures)
So if that operation is going to take so long,
2:58
S… Speaker 1 (4- Callables and Futures)
perhaps we don't want to wait forever.
2:59
S… Speaker 1 (4- Callables and Futures)
We want to timeout in,
3:01
S… Speaker 1 (4- Callables and Futures)
let's say, two seconds.
3:02
S… Speaker 1 (4- Callables and Futures)
We can also call the get method without a timeout value.
3:05
S… Speaker 1 (4- Callables and Futures)
We have cancel for canceling an operation.
3:09
S… Speaker 1 (4- Callables and Futures)
we have is cancelled with this we can check to see if this operation is cancelled
3:13
S… Speaker 1 (4- Callables and Futures)
and is done which tells us if this operation is complete or not.
3:17
S… Speaker 1 (4- Callables and Futures)
So I told you that this submit method is going to return immediately.
3:21
S… Speaker 1 (4- Callables and Futures)
So here we can do some more work.
3:24
S… Speaker 1 (4- Callables and Futures)
Let's say do more work and at some point
3:28
S… Speaker 1 (4- Callables and Futures)
you really need the result of this operation.
3:30
S… Speaker 1 (4- Callables and Futures)
That is when we call the get method.
3:34
S… Speaker 1 (4- Callables and Futures)
Now this method is going to block the current thread and have it wait until the
3:38
S… Speaker 1 (4- Callables and Futures)
result of this operation is ready.
3:40
S… Speaker 1 (4- Callables and Futures)
Eventually,
3:41
S… Speaker 1 (4- Callables and Futures)
it's going to return a value.
3:42
S… Speaker 1 (4- Callables and Futures)
In this case,
3:43
S… Speaker 1 (4- Callables and Futures)
it's going to return an integer because our task returns
3:47
S… Speaker 1 (4- Callables and Futures)
an integer.
3:47
S… Speaker 1 (4- Callables and Futures)
So let's store the result over here.
3:50
S… Speaker 2 (4- Callables and Futures)
Take a look.
3:52
S… Speaker 1 (4- Callables and Futures)
The type of this variable is integer.
3:55
S… Speaker 2 (4- Callables and Futures)
Okay?
3:55
S… Speaker 1 (4- Callables and Futures)
Now here we need to handle a couple of exceptions.
4:01
S… Speaker 1 (4- Callables and Futures)
One of them is interrupted exception that you have seen before.
4:05
S… Speaker 1 (4- Callables and Futures)
The other is execution exception.
4:07
S… Speaker 1 (4- Callables and Futures)
And this happens if this task throws an exception.
4:11
S… Speaker 1 (4- Callables and Futures)
So we can cache this exception over here and do something with it.
4:15
S… Speaker 1 (4- Callables and Futures)
Now in this demo I'm going to simplify this code and combine
4:19
S… Speaker 1 (4- Callables and Futures)
these catch blocks.
4:20
S… Speaker 1 (4- Callables and Futures)
So vertical bar,
4:21
S… Speaker 1 (4- Callables and Futures)
execution,
4:22
S… Speaker 1 (4- Callables and Futures)
exception.
4:23
S… Speaker 1 (4- Callables and Futures)
Now let's get rid of the second catch block.
4:28
S… Speaker 1 (4- Callables and Futures)
So, when I run this program,
4:30
S… Speaker 1 (4- Callables and Futures)
you will see that we immediately see this message on a terminal because the submit
4:34
S… Speaker 1 (4- Callables and Futures)
method is not going to wait for the completion of this task.
4:37
S… Speaker 1 (4- Callables and Futures)
It's going to start this task on a separate thread and eventually when we
4:41
S… Speaker 1 (4- Callables and Futures)
call future .get,
4:42
S… Speaker 1 (4- Callables and Futures)
we have to wait for this operation to complete.
4:45
S… Speaker 1 (4- Callables and Futures)
Now we get the result and print it.
4:48
S… Speaker 2 (4- Callables and Futures)
Let's see what happens.
4:50
S… Speaker 1 (4- Callables and Futures)
So,
4:51
S… Speaker 1 (4- Callables and Futures)
we see do more work,
4:53
S… Speaker 1 (4- Callables and Futures)
now 1,
4:53
S… Speaker 2 (4- Callables and Futures)
2,
4:54
S… Speaker 1 (4- Callables and Futures)
3 seconds later,
4:55
S… Speaker 1 (4- Callables and Futures)
the result is ready and we print it over here.

Denna utskrift genererades av AI (automatisk taligenkänning). Kan innehålla fel — verifiera mot originalljudet för kritisk användning. Politik för AI

❤️ Älskar du STT.ai? Berätta för dina vänner!
Sammanfattning
Klicka på Summarize för att generera en AI sammanfattning av denna utskrift.
Sammanfatta...
Fråga AI om detta Transcript
Fråga något om denna utskrift — AI kommer att hitta relevanta avsnitt och svar.