Kuonyesha tu
0:03
S… Speaker 2 (3- Starting a Thread)
To create a thread in Java,
0:05
S… Speaker 2 (3- Starting a Thread)
we use the thread class that is declared in the java .lang package.
0:09
S… Speaker 2 (3- Starting a Thread)
So let's create a new thread object,
0:11
S… Speaker 1 (3- Starting a Thread)
new thread.
0:13
S… Speaker 1 (3- Starting a Thread)
Now,
0:14
S… Speaker 2 (3- Starting a Thread)
the constructor of this class is overloaded.
0:16
S… Speaker 2 (3- Starting a Thread)
The version that we use most often is the one that takes a runnable object.
0:20
S… Speaker 2 (3- Starting a Thread)
That is an object that implements the runnable interface.
0:23
S… Speaker 2 (3- Starting a Thread)
This runnable interface represents a task to be run on
0:27
S… Speaker 1 (3- Starting a Thread)
a thread.
0:27
S… Speaker 2 (3- Starting a Thread)
It's a very simple interface with a single method called run.
0:31
S… Speaker 2 (3- Starting a Thread)
So it has no parameters and returns void.
0:34
S… Speaker 2 (3- Starting a Thread)
So let's say we want to download many files concurrently.
0:38
S… Speaker 2 (3- Starting a Thread)
So the task that we're dealing with here is the task of downloading a file.
0:42
S… Speaker 2 (3- Starting a Thread)
So let's add a new class to our project.
0:45
S… Speaker 2 (3- Starting a Thread)
I'm going to call this download file task
0:49
S… Speaker 2 (3- Starting a Thread)
and have this class implement the runnable interface.
0:53
S… Speaker 1 (3- Starting a Thread)
Now here for simplicity,
0:57
S… Speaker 2 (3- Starting a Thread)
let's just print a message like downloading a
1:01
S… Speaker 1 (3- Starting a Thread)
file.
1:02
S… Speaker 2 (3- Starting a Thread)
So let's not worry about the complexities of downloading a file.
1:05
S… Speaker 2 (3- Starting a Thread)
We just want to focus on concurrency.
1:07
S… Speaker 1 (3- Starting a Thread)
Now,
1:08
S… Speaker 1 (3- Starting a Thread)
back to our demo class,
1:09
S… Speaker 2 (3- Starting a Thread)
let's create a new thread object.
1:12
S… Speaker 2 (3- Starting a Thread)
Here we should pass a new download file
1:16
S… Speaker 1 (3- Starting a Thread)
task.
1:16
S… Speaker 2 (3- Starting a Thread)
So this is how we can create a new thread.
1:20
S… Speaker 2 (3- Starting a Thread)
Now we call thread .start to start this thread.
1:24
S… Speaker 1 (3- Starting a Thread)
Now,
1:25
S… Speaker 2 (3- Starting a Thread)
when I run this program,
1:26
S… Speaker 2 (3- Starting a Thread)
the code for downloading a file will be executed in a separate thread.
1:29
S… Speaker 2 (3- Starting a Thread)
Let me show you how to verify it.
1:32
S… Speaker 2 (3- Starting a Thread)
So before we create a thread,
1:33
S… Speaker 2 (3- Starting a Thread)
let's print the name of the current thread.
1:36
S… Speaker 2 (3- Starting a Thread)
That is the thread that executes our main method.
1:39
S… Speaker 2 (3- Starting a Thread)
So print,
1:40
S… Speaker 1 (3- Starting a Thread)
here we call thread .currentThread
1:44
S… Speaker 1 (3- Starting a Thread)
.getName.
1:46
S… Speaker 1 (3- Starting a Thread)
So this static method,
1:48
S… Speaker 1 (3- Starting a Thread)
currentThread,
1:49
S… Speaker 1 (3- Starting a Thread)
returns the current thread.
1:50
S… Speaker 2 (3- Starting a Thread)
Each thread has a name and an ID.
1:52
S… Speaker 1 (3- Starting a Thread)
So we can call it getName or getID.
1:56
S… Speaker 1 (3- Starting a Thread)
In this case I'm going to call getName.
1:58
S… Speaker 1 (3- Starting a Thread)
Now back to our task class.
2:00
S… Speaker 2 (3- Starting a Thread)
Let's print the name of the current thread while downloading a file.
2:04
S… Speaker 2 (3- Starting a Thread)
So over here Let's append thread that
2:08
S… Speaker 1 (3- Starting a Thread)
current thread that get name Okay,
2:11
S… Speaker 1 (3- Starting a Thread)
now let's run our program So we
2:18
S… Speaker 2 (3- Starting a Thread)
have two threads one is the main thread that executed our main method and
2:22
S… Speaker 2 (3- Starting a Thread)
here's another thread that we explicitly created for downloading a file Each
2:27
S… Speaker 2 (3- Starting a Thread)
thread starts executes a task and then dies so we don't have to explicitly kill
2:31
S… Speaker 2 (3- Starting a Thread)
it Now let's make this example more interesting
2:35
S… Speaker 2 (3- Starting a Thread)
Let's wrap this inside a for loop to simulate downloading 10 files
2:39
S… Speaker 1 (3- Starting a Thread)
concurrently.
2:40
S… Speaker 2 (3- Starting a Thread)
So I'm going to add a for loop,
2:41
S… Speaker 1 (3- Starting a Thread)
4,
2:42
S… Speaker 2 (3- Starting a Thread)
I is 0,
2:44
S… Speaker 2 (3- Starting a Thread)
as long as it's less than 10,
2:46
S… Speaker 1 (3- Starting a Thread)
I plus plus,
2:47
S… Speaker 1 (3- Starting a Thread)
and here we start 10
2:51
S… Speaker 1 (3- Starting a Thread)
threads.
2:52
S… Speaker 2 (3- Starting a Thread)
Let's run our program again.
2:54
S… Speaker 1 (3- Starting a Thread)
Take a look.
2:56
S… Speaker 2 (3- Starting a Thread)
We have the main thread plus 10 extra threads for
3:00
S… Speaker 2 (3- Starting a Thread)
downloading 10 files concurrently.
3:03
S… Speaker 2 (3- Starting a Thread)
Now, even though these messages are appearing sequentially on the terminal,
3:06
S… Speaker 2 (3- Starting a Thread)
these threads start and run in parallel.
3:09
S… Speaker 2 (3- Starting a Thread)
So this is how we can create and start a thread in Java.

Nakala hii ilitokezwa na AI (utambuaji wa usemi wa kiaya). Inaweza kuwa na makosa Équipe dhidi ya sauti ya awali kwa utumizi wa kuchambua. Sera ya AI

❤️ Love STT.ai? Tell your friends!
Muhtasari
Bonyeza muhtasari ili kutokeza muhtasari wa AI juu ya nakala hii.
Kutoa muhtasari...
Uliza Maswali Kuhusu Mpito Huu
Uliza jambo lolote kuhusu nakala hii ya kitabu KULEA ile nitakayopata sehemu zinazofaa na majibu.