3- Executors
Jul 03, 2026 22:57
· 6:50
· English
· Whisper Turbo
· 2 Kelliema
Din it-tranżazzjoni tiskadi llum.
Aġġornament għall-ħażna permanenti →
Li juru biss
0:03
S…
Speaker 2 (3- Executors)
In Java,
0:04
S…
Speaker 1 (3- Executors)
the concept of a thread pool is represented using the executor service interface
0:08
S…
Speaker 1 (3- Executors)
and its implementations.
0:10
S…
Speaker 1 (3- Executors)
So we have thread pool executor,
0:12
S…
Speaker 1 (3- Executors)
which is a typical thread pool implementation.
0:15
S…
Speaker 1 (3- Executors)
This is the one that we use most of the time.
0:17
S…
Speaker 1 (3- Executors)
We also have scheduled thread pool executor.
0:20
S…
Speaker 1 (3- Executors)
With this,
0:21
S…
Speaker 1 (3- Executors)
we can schedule tasks to run after a delay or periodically.
0:24
S…
Speaker 1 (3- Executors)
For example,
0:26
S…
Speaker 1 (3- Executors)
we can schedule a task to run five hours from now or every two hours.
0:29
S…
Speaker 1 (3- Executors)
We also have fork join pool.
0:33
S…
Speaker 1 (3- Executors)
This is a special type of pool that is designed to recursively split a task
0:37
S…
Speaker 1 (3- Executors)
into smaller tasks and then combine the results of each subtask to
0:41
S…
Speaker 1 (3- Executors)
produce the overall result.
0:43
S…
Speaker 1 (3- Executors)
It's like a divide and conquer algorithm.
0:46
S…
Speaker 1 (3- Executors)
Now let me show you how to create a thread pool.
0:48
S…
Speaker 1 (3- Executors)
We have this executors class that is declared in the java
0:52
S…
Speaker 1 (3- Executors)
.util .concurrent package.
0:53
S…
Speaker 1 (3- Executors)
This class has a bunch of static factory methods for creating
0:58
S…
Speaker 1 (3- Executors)
an executor service.
0:59
S…
Speaker 1 (3- Executors)
So with these methods we can create an instance of these implementations.
1:03
S…
Speaker 1 (3- Executors)
Now, why shouldn't we create a new instance of thread pool executor
1:08
S…
Speaker 1 (3- Executors)
directly?
1:08
S…
Speaker 1 (3- Executors)
We can definitely do that,
1:09
S…
Speaker 1 (3- Executors)
but it's a little bit difficult.
1:10
S…
Speaker 2 (3- Executors)
Let me show you.
1:11
S…
Speaker 1 (3- Executors)
So if you type new thread pool executor,
1:16
S…
Speaker 2 (3- Executors)
take a look.
1:17
S…
Speaker 1 (3- Executors)
This constructor has a bunch of parameters like core pool size,
1:21
S…
Speaker 1 (3- Executors)
maximum pool size,
1:23
S…
Speaker 1 (3- Executors)
keep a lifetime,
1:24
S…
Speaker 2 (3- Executors)
and so on.
1:24
S…
Speaker 1 (3- Executors)
So creating an executor explicitly is a little bit difficult.
1:28
S…
Speaker 1 (3- Executors)
That is why we use the factory methods on the executors class.
1:33
S…
Speaker 1 (3- Executors)
So here we can call new single thread executor.
1:36
S…
Speaker 1 (3- Executors)
This returns an executor with a single thread.
1:38
S…
Speaker 1 (3- Executors)
That's something that we use that often.
1:40
S…
Speaker 1 (3- Executors)
We also have new fixed thread pool.
1:43
S…
Speaker 1 (3- Executors)
This will create a thread pool with a given number of worker threads.
1:46
S…
Speaker 1 (3- Executors)
So that would be an instance of the thread pool executor
1:51
S…
Speaker 1 (3- Executors)
class.
1:51
S…
Speaker 1 (3- Executors)
We also have new schedule thread pool.
1:54
S…
Speaker 1 (3- Executors)
This will return
1:56
S…
Speaker 1 (3- Executors)
instance of the scheduled thread pool executor.
1:59
S…
Speaker 1 (3- Executors)
Okay, so let's call new fixed thread pool
2:03
S…
Speaker 1 (3- Executors)
with two worker threads.
2:05
S…
Speaker 1 (3- Executors)
Now we have stored a result in a variable called executor.
2:09
S…
Speaker 1 (3- Executors)
Now look at the type of this variable.
2:11
S…
Speaker 1 (3- Executors)
That is executor service.
2:14
S…
Speaker 1 (3- Executors)
So we're dealing with an interface here.
2:15
S…
Speaker 1 (3- Executors)
At runtime,
2:17
S…
Speaker 1 (3- Executors)
the type of this object is going to be thread pool executor.
2:20
S…
Speaker 2 (3- Executors)
Let me show you.
2:21
S…
Speaker 1 (3- Executors)
So if you print executor
2:25
S…
Speaker 1 (3- Executors)
.git class
2:27
S…
Speaker 1 (3- Executors)
but get name and run the program we see
2:31
S…
Speaker 1 (3- Executors)
thread pool executor that is a thread pool with a number of worker threats.
2:35
S…
Speaker 1 (3- Executors)
Okay, so we have an executor
2:40
S…
Speaker 1 (3- Executors)
now we can call executor .submit
2:44
S…
Speaker 1 (3- Executors)
to submit a task to this thread pool.
2:46
S…
Speaker 1 (3- Executors)
Now this method is overloaded.
2:48
S…
Speaker 1 (3- Executors)
We can pass a runnable object.
2:50
S…
Speaker 1 (3- Executors)
We can also pass a callable.
2:52
S…
Speaker 1 (3- Executors)
That is a task that returns a result.
2:54
S…
Speaker 1 (3- Executors)
We'll look at that later.
2:55
S…
Speaker 1 (3- Executors)
So for now,
2:56
S…
Speaker 1 (3- Executors)
let's pass a runnable object here.
2:58
S…
Speaker 1 (3- Executors)
I'm going to use a lambda expression.
3:01
S…
Speaker 1 (3- Executors)
So here,
3:02
S…
Speaker 1 (3- Executors)
let's print the name of the current thread.
3:05
S…
Speaker 1 (3- Executors)
So thread .currentThread .getName
3:10
S…
Speaker 1 (3- Executors)
Now technically we don't need the braces here because we have a single print line statement,
3:14
S…
Speaker 1 (3- Executors)
but I'm going to keep them here for clarity because I don't want to put this line over
3:19
S…
Speaker 1 (3- Executors)
here, otherwise it's going to pop out of the screen.
3:21
S…
Speaker 1 (3- Executors)
So that's all we have to do to run a task on a separate
3:25
S…
Speaker 1 (3- Executors)
thread.
3:26
S…
Speaker 1 (3- Executors)
If you run this program,
3:27
S…
Speaker 1 (3- Executors)
you can see our task was executed on this
3:31
S…
Speaker 1 (3- Executors)
thread. Pool one,
3:32
S…
Speaker 1 (3- Executors)
thread one.
3:33
S…
Speaker 1 (3- Executors)
So we didn't have to explicitly create a thread.
3:36
S…
Speaker 1 (3- Executors)
Now if you have a thousand tasks,
3:38
S…
Speaker 1 (3- Executors)
we don't have to worry about creating too many threads and running out of memory.
3:42
S…
Speaker 1 (3- Executors)
We simply submit those tasks to this executor,
3:45
S…
Speaker 2 (3- Executors)
to this thread pool,
3:46
S…
Speaker 1 (3- Executors)
and this pool will assign our tasks to work your threads.
3:49
S…
Speaker 2 (3- Executors)
Let me show you.
3:50
S…
Speaker 1 (3- Executors)
So here we have a pool with two threads.
3:52
S…
Speaker 1 (3- Executors)
Let's add a for loop for i,
3:56
S…
Speaker 1 (3- Executors)
we start from 0 to let's say 10.
3:58
S…
Speaker 1 (3- Executors)
Now in each iteration we submit
4:03
S…
Speaker 1 (3- Executors)
a task to this pool.
4:04
S…
Speaker 1 (3- Executors)
So we have only two threads available,
4:06
S…
Speaker 1 (3- Executors)
but we're submitting 10 tasks.
4:08
S…
Speaker 1 (3- Executors)
Let's see what happens.
4:09
S…
Speaker 2 (3- Executors)
Take a
4:13
S…
Speaker 2 (3- Executors)
look. So,
4:14
S…
Speaker 1 (3- Executors)
some tasks are being executed on thread one,
4:16
S…
Speaker 1 (3- Executors)
other tasks are being executed on thread two.
4:19
S…
Speaker 2 (3- Executors)
See?
4:20
S…
Speaker 1 (3- Executors)
So internally this executor maintains a queue.
4:23
S…
Speaker 1 (3- Executors)
Every task that we submit goes in this queue and waits for an available
4:27
S…
Speaker 1 (3- Executors)
thread.
4:28
S…
Speaker 2 (3- Executors)
Okay?
4:28
S…
Speaker 1 (3- Executors)
Now, let's get rid of this for loop.
4:30
S…
Speaker 1 (3- Executors)
We don't need it anymore.
4:32
S…
Speaker 1 (3- Executors)
So we create an executor and submit a task.
4:36
S…
Speaker 1 (3- Executors)
Now, if I run this program again,
4:38
S…
Speaker 1 (3- Executors)
we get this dialog box saying that our program is still running.
4:41
S…
Speaker 1 (3- Executors)
Why is that?
4:42
S…
Speaker 1 (3- Executors)
Because when we start an executor and submit a task,
4:45
S…
Speaker 1 (3- Executors)
the executor thinks there might be more tasks coming in the future.
4:49
S…
Speaker 1 (3- Executors)
So it's not going to terminate.
4:51
S…
Speaker 1 (3- Executors)
It's going to stay in the memory waiting for new tasks.
4:53
S…
Speaker 1 (3- Executors)
So we have to explicitly shut down an executor to terminate
4:58
S…
Speaker 2 (3- Executors)
our program.
4:58
S…
Speaker 1 (3- Executors)
And to do that we call executor .shutdown.
5:02
S…
Speaker 1 (3- Executors)
We also have shutdown now.
5:04
S…
Speaker 1 (3- Executors)
The difference is that the shutdown method doesn't stop the current tasks.
5:08
S…
Speaker 1 (3- Executors)
So it will wait for the completion of those tasks,
5:11
S…
Speaker 1 (3- Executors)
but it's not going to accept any new tasks.
5:13
S…
Speaker 1 (3- Executors)
In contrast,
5:14
S…
Speaker 1 (3- Executors)
if you call shutdown now,
5:16
S…
Speaker 1 (3- Executors)
this will force the existing tasks to stop.
5:18
S…
Speaker 1 (3- Executors)
Okay, so let's call the shutdown method.
5:22
S…
Speaker 1 (3- Executors)
Now we run the program.
5:23
S…
Speaker 1 (3- Executors)
We get this dialog box because we didn't shut down our executor previously.
5:27
S…
Speaker 1 (3- Executors)
So let's rerun this program.
5:31
S…
Speaker 1 (3- Executors)
All right, our program shut down.
5:32
S…
Speaker 1 (3- Executors)
So if I run it again,
5:34
S…
Speaker 1 (3- Executors)
we don't get that warning.
5:35
S…
Speaker 1 (3- Executors)
Okay.
5:35
S…
Speaker 2 (3- Executors)
Now,
5:36
S…
Speaker 1 (3- Executors)
what if something goes wrong over here?
5:41
S…
Speaker 1 (3- Executors)
Or we might have some code and
5:45
S…
Speaker 1 (3- Executors)
this code throws an exception.
5:47
S…
Speaker 1 (3- Executors)
With that,
5:48
S…
Speaker 1 (3- Executors)
we're not going to be able to shut down an executor properly.
5:51
S…
Speaker 1 (3- Executors)
So as a best practice,
5:53
S…
Speaker 1 (3- Executors)
we should run this inside a try find block.
5:56
S…
Speaker 1 (3- Executors)
So I'm going to add a try block here and the
6:00
S…
Speaker 1 (3- Executors)
try block
6:01
S…
Speaker 1 (3- Executors)
we submit our task and in
6:05
S…
Speaker 1 (3- Executors)
the final block,
6:06
S…
Speaker 1 (3- Executors)
we shut down the executor.
6:10
S…
Speaker 1 (3- Executors)
With this,
6:11
S…
Speaker 1 (3- Executors)
we'll make sure that no matter what,
6:13
S…
Speaker 1 (3- Executors)
we always shut down an executor and release it from the memory.
6:18
S…
Speaker 1 (3- Executors)
So this is the benefit of using the executor framework.
6:20
S…
Speaker 1 (3- Executors)
We don't have to worry about threat manipulation anymore.
6:23
S…
Speaker 1 (3- Executors)
We let Java,
6:24
S…
Speaker 1 (3- Executors)
we let the executor framework take care of all of that.
6:26
S…
Speaker 1 (3- Executors)
But you have to remember,
6:28
S…
Speaker 1 (3- Executors)
even when using the executor framework,
6:30
S…
Speaker 1 (3- Executors)
we still have to worry about the concurrency problems.
6:32
S…
Speaker 1 (3- Executors)
So if two tasks modify an object
6:36
S…
Speaker 1 (3- Executors)
concurrently, we're going to run into issues.
6:38
S…
Speaker 1 (3- Executors)
So the executor framework does not protect us from the concurrency problems
6:42
S…
Speaker 1 (3- Executors)
we talked about in the last section.
6:44
S…
Speaker 1 (3- Executors)
It just simplifies threat manipulation.
Din it-tranżmissjoni ġiet iġġenerata minn AI (rikonoxximent awtomatiku tad-diskors) jista' jkun fiha żbalji — ivverifika mal-awdjo oriġinali għal użu kritiku. Politika dwar l-AI
Sommarju
Ikklikkja Sommarju biex tiġġenera sommarju AI ta ’dan it-traskrizzjoni.
Fil-qosor...
Staqsi AI Dwar Dan Tranżkript
Staqsi xi ħaġa dwar din it-traskrizzjoni — l-AI se ssib sezzjonijiet rilevanti u twieġeb.