7- Creating a Completable Future
Jul 03, 2026 23:04
· 3:42
· English
· Whisper Turbo
· 2 بلندگوها
اين رونوشت امروز تموم ميشه
ارتقا برای ذخیرهسازی دائمی →
نمایش فقط
0:03
S…
Speaker 2 (7- Creating a Completable Future)
This completable future class has a couple of methods that we use for creating a
0:07
S…
Speaker 2 (7- Creating a Completable Future)
completable future object.
0:09
S…
Speaker 2 (7- Creating a Completable Future)
If you want to run a task that doesn't return a value we call run async.
0:13
S…
Speaker 2 (7- Creating a Completable Future)
This basically says run this task in an asynchronous or non
0:17
S…
Speaker 1 (7- Creating a Completable Future)
-blocking fashion.
0:18
S…
Speaker 2 (7- Creating a Completable Future)
Here we can pass a runnable object or
0:22
S…
Speaker 2 (7- Creating a Completable Future)
a runnable plus an executor.
0:25
S…
Speaker 2 (7- Creating a Completable Future)
If you don't pass an executor,
0:27
S…
Speaker 2 (7- Creating a Completable Future)
this method is going to run our task on a common pool.
0:30
S…
Speaker 1 (7- Creating a Completable Future)
Let me show you.
0:31
S…
Speaker 2 (7- Creating a Completable Future)
So here we have this class fork join pool.
0:34
S…
Speaker 2 (7- Creating a Completable Future)
This is one of the implementations of the executor service interface.
0:38
S…
Speaker 2 (7- Creating a Completable Future)
Now here we have a static method common pool and this
0:42
S…
Speaker 2 (7- Creating a Completable Future)
returns the pool that is used by the completable future class.
0:45
S…
Speaker 2 (7- Creating a Completable Future)
So if you don't supply a pool,
0:47
S…
Speaker 2 (7- Creating a Completable Future)
this is the pool that is going to be used under the hood.
0:50
S…
Speaker 2 (7- Creating a Completable Future)
Now this pool is well aware of the number of available threads.
0:54
S…
Speaker 2 (7- Creating a Completable Future)
Earlier I told you how to get that.
0:55
S…
Speaker 2 (7- Creating a Completable Future)
So we have this class runtime.
0:59
S…
Speaker 2 (7- Creating a Completable Future)
we call it get runtime and then available processors.
1:03
S…
Speaker 2 (7- Creating a Completable Future)
So on my machine I have four cores and each core has two threads.
1:07
S…
Speaker 2 (7- Creating a Completable Future)
So the common pool is based on the number of available threads.
1:10
S…
Speaker 2 (7- Creating a Completable Future)
Now if this doesn't fit your application,
1:13
S…
Speaker 2 (7- Creating a Completable Future)
you can always create a custom pool with fewer or more threads.
1:16
S…
Speaker 2 (7- Creating a Completable Future)
Okay, now to simplify this,
1:19
S…
Speaker 2 (7- Creating a Completable Future)
I'm not going to pass a pool,
1:21
S…
Speaker 2 (7- Creating a Completable Future)
I'm just going to pass a runnable object.
1:23
S…
Speaker 2 (7- Creating a Completable Future)
So let's create a runnable object called task.
1:28
S…
Speaker 1 (7- Creating a Completable Future)
And here we just want to print something.
1:30
S…
Speaker 1 (7- Creating a Completable Future)
Now we can pass the task over here.
1:35
S…
Speaker 2 (7- Creating a Completable Future)
Now this returns a completable future object.
1:38
S…
Speaker 2 (7- Creating a Completable Future)
Now look at the type of this object.
1:40
S…
Speaker 2 (7- Creating a Completable Future)
It's a completable future of void.
1:43
S…
Speaker 2 (7- Creating a Completable Future)
Because our task is not returning a value.
1:46
S…
Speaker 1 (7- Creating a Completable Future)
Okay?
1:46
S…
Speaker 1 (7- Creating a Completable Future)
Now
1:48
S…
Speaker 2 (7- Creating a Completable Future)
With this single line of code,
1:49
S…
Speaker 2 (7- Creating a Completable Future)
we can execute a task in an asynchronous fashion.
1:52
S…
Speaker 2 (7- Creating a Completable Future)
We didn't have to create an executor and then submit a task to it
1:56
S…
Speaker 2 (7- Creating a Completable Future)
and then shut it down.
1:57
S…
Speaker 2 (7- Creating a Completable Future)
We only call completable future that run async.
2:01
S…
Speaker 2 (7- Creating a Completable Future)
If our task would return a value instead of calling the run async
2:05
S…
Speaker 2 (7- Creating a Completable Future)
method, recall the supply async method.
2:08
S…
Speaker 2 (7- Creating a Completable Future)
Now look at the signature of this method.
2:11
S…
Speaker 2 (7- Creating a Completable Future)
Here we can pass a supplier
2:13
S…
Speaker 2 (7- Creating a Completable Future)
That is a functional interface.
2:14
S…
Speaker 2 (7- Creating a Completable Future)
We talked about it before.
2:16
S…
Speaker 2 (7- Creating a Completable Future)
It has a single abstract method called get that returns a value.
2:20
S…
Speaker 2 (7- Creating a Completable Future)
Now similar to the run async method,
2:23
S…
Speaker 2 (7- Creating a Completable Future)
here we can pass a supplier or a supplier plus an
2:27
S…
Speaker 1 (7- Creating a Completable Future)
executor.
2:27
S…
Speaker 2 (7- Creating a Completable Future)
If we don't supply an executor,
2:29
S…
Speaker 1 (7- Creating a Completable Future)
the common pool will be used.
2:31
S…
Speaker 2 (7- Creating a Completable Future)
Okay, so I'm going to change this task to a supplier
2:35
S…
Speaker 1 (7- Creating a Completable Future)
of integer and here let's
2:39
S…
Speaker 1 (7- Creating a Completable Future)
just return one.
2:43
S…
Speaker 2 (7- Creating a Completable Future)
this will return a completable future of integer.
2:46
S…
Speaker 1 (7- Creating a Completable Future)
Let me show you.
2:46
S…
Speaker 1 (7- Creating a Completable Future)
So the type of this future object
2:50
S…
Speaker 2 (7- Creating a Completable Future)
is completable future of integer.
2:53
S…
Speaker 1 (7- Creating a Completable Future)
Now,
2:54
S…
Speaker 2 (7- Creating a Completable Future)
if you want to get this value,
2:55
S…
Speaker 2 (7- Creating a Completable Future)
we call future .get because this is a future object.
2:59
S…
Speaker 2 (7- Creating a Completable Future)
So all the methods that you learn about future objects also exist here.
3:04
S…
Speaker 2 (7- Creating a Completable Future)
As I told you before,
3:05
S…
Speaker 2 (7- Creating a Completable Future)
this get method is a blocking method.
3:07
S…
Speaker 2 (7- Creating a Completable Future)
So it's going to block the current thread until the result is ready.
3:10
S…
Speaker 2 (7- Creating a Completable Future)
But in this completable future object,
3:12
S…
Speaker 2 (7- Creating a Completable Future)
we have a bunch of other methods that allow us to build complex asynchronous
3:17
S…
Speaker 2 (7- Creating a Completable Future)
operations.
3:18
S…
Speaker 2 (7- Creating a Completable Future)
We'll talk about them as you go through this section.
3:20
S…
Speaker 2 (7- Creating a Completable Future)
Now let's call the get method and store the result
3:24
S…
Speaker 1 (7- Creating a Completable Future)
over here.
3:25
S…
Speaker 1 (7- Creating a Completable Future)
We should wrap this with a try catch block and
3:29
S…
Speaker 1 (7- Creating a Completable Future)
then print the result.
3:32
S…
Speaker 1 (7- Creating a Completable Future)
There you go.
3:35
S…
Speaker 2 (7- Creating a Completable Future)
So this is how we can create a completable feature object.
این رونوشت توسط AI (شناسایی خودکار گفتار) تولید شد. ممکن است حاوی خطا باشد — برای استفادهٔ حیاتی با صدای اصلی بررسی کنید. سیاست هوش مصنوعی
خلاصه
برای تولید خلاصهای از این رونوشت از هوش مصنوعی ، خلاصه را فشار دهید.
خلاصه کنم...
از هوش مصنوعی در مورد این رونوشت بپرسید
در این روش، هر سؤالی که در مورد این رونوشت پرسیده شود، هوش مصنوعی بخشهای مربوطه را پیدا کرده و پاسخ میدهد.