Mung nyritakake
0:03
S… Speaker 2 (5- Joining a Thread)
Let's say once we download a file we want to kick off another thread to scan the
0:07
S… Speaker 2 (5- Joining a Thread)
downloaded file for viruses.
0:09
S… Speaker 2 (5- Joining a Thread)
Now scanning a file is a time -consuming task It's not going to happen immediately
0:13
S… Speaker 2 (5- Joining a Thread)
like incrementing an integer.
0:14
S… Speaker 2 (5- Joining a Thread)
It may take a few seconds So we should run it in a separate thread But
0:19
S… Speaker 2 (5- Joining a Thread)
we should do that only after we have downloaded a file.
0:22
S… Speaker 1 (5- Joining a Thread)
Here's the thing.
0:24
S… Speaker 2 (5- Joining a Thread)
How can we know if the download thread has finished?
0:27
S… Speaker 2 (5- Joining a Thread)
downloading a file may take a second,
0:29
S… Speaker 2 (5- Joining a Thread)
a minute, or longer.
0:30
S… Speaker 2 (5- Joining a Thread)
We cannot predict it.
0:31
S… Speaker 2 (5- Joining a Thread)
So here we cannot use thread .sleep to make the current thread sleep
0:35
S… Speaker 2 (5- Joining a Thread)
for, let's say,
0:36
S… Speaker 2 (5- Joining a Thread)
five seconds,
0:37
S… Speaker 2 (5- Joining a Thread)
because the download may take longer.
0:39
S… Speaker 2 (5- Joining a Thread)
To solve this problem,
0:41
S… Speaker 2 (5- Joining a Thread)
we should join threads.
0:42
S… Speaker 2 (5- Joining a Thread)
So after we start the download thread,
0:45
S… Speaker 2 (5- Joining a Thread)
we call thread .join,
0:48
S… Speaker 2 (5- Joining a Thread)
and this will make the current thread,
0:50
S… Speaker 2 (5- Joining a Thread)
that is the main thread that is executing this code,
0:52
S… Speaker 2 (5- Joining a Thread)
wait for the completion of the download thread.
0:56
S… Speaker 2 (5- Joining a Thread)
So this call is going to block the current thread until the download thread has
1:00
S… Speaker 1 (5- Joining a Thread)
finished.
1:00
S… Speaker 2 (5- Joining a Thread)
Once this method returns,
1:02
S… Speaker 2 (5- Joining a Thread)
then we can print a message like file is ready to
1:07
S… Speaker 1 (5- Joining a Thread)
be scanned.
1:07
S… Speaker 1 (5- Joining a Thread)
Now,
1:09
S… Speaker 2 (5- Joining a Thread)
just like the sleep method,
1:10
S… Speaker 2 (5- Joining a Thread)
the join method throws an interrupted exception,
1:13
S… Speaker 2 (5- Joining a Thread)
so we should handle it here.
1:15
S… Speaker 1 (5- Joining a Thread)
Let's wrap it with a try -catch block.
1:17
S… Speaker 1 (5- Joining a Thread)
Alright,
1:18
S… Speaker 1 (5- Joining a Thread)
so we start a download thread.
1:23
S… Speaker 2 (5- Joining a Thread)
then we join the current thread with that thread,
1:25
S… Speaker 2 (5- Joining a Thread)
so this will make the current thread wait for the completion of that thread,
1:29
S… Speaker 2 (5- Joining a Thread)
and then we print this message.
1:31
S… Speaker 1 (5- Joining a Thread)
Let's run the program,
1:32
S… Speaker 1 (5- Joining a Thread)
so download started,
1:35
S… Speaker 2 (5- Joining a Thread)
now 1,
1:36
S… Speaker 1 (5- Joining a Thread)
2,
1:36
S… Speaker 1 (5- Joining a Thread)
3,
1:37
S… Speaker 2 (5- Joining a Thread)
4,
1:38
S… Speaker 2 (5- Joining a Thread)
5 seconds later,
1:39
S… Speaker 2 (5- Joining a Thread)
download is complete,
1:40
S… Speaker 2 (5- Joining a Thread)
and we see that the file is ready to be scanned.
1:43
S… Speaker 2 (5- Joining a Thread)
So with the join method,
1:45
S… Speaker 2 (5- Joining a Thread)
we tell the current thread to wait for the completion of another thread,
1:48
S… Speaker 2 (5- Joining a Thread)
and that means,
1:49
S… Speaker 2 (5- Joining a Thread)
while this thread is waiting,
1:51
S… Speaker 2 (5- Joining a Thread)
it cannot do other things.
1:53
S… Speaker 2 (5- Joining a Thread)
For example,
1:54
S… Speaker 2 (5- Joining a Thread)
in the case of a desktop or a mobile app,
1:56
S… Speaker 2 (5- Joining a Thread)
this main thread is responsible for handling UI events like mouse
2:00
S… Speaker 2 (5- Joining a Thread)
clicks and keystrokes.
2:01
S… Speaker 2 (5- Joining a Thread)
So if you make the main thread wait for another thread,
2:04
S… Speaker 2 (5- Joining a Thread)
it cannot respond to these UI events while it's waiting.
2:07
S… Speaker 2 (5- Joining a Thread)
So the UI will freeze and the user will not be able to resize or move
2:11
S… Speaker 2 (5- Joining a Thread)
the application window.
2:12
S… Speaker 2 (5- Joining a Thread)
But don't worry,
2:13
S… Speaker 2 (5- Joining a Thread)
later I will show you a better way to have one task start after another
2:17
S… Speaker 2 (5- Joining a Thread)
task finishes without causing the current thread to wait.
2:21
S… Speaker 2 (5- Joining a Thread)
For now I just want you to understand what joining is and how it works.

Transkripsi iki digawé déning AI (pengidentifikasi swara otomatis). Bisa uga ana cacat - verifikasi karo audio asli kanggo panggunaan kritis. Kebijakan AI

❤️ Cinta STT.ai? Nyathet kanca-kancamu!
Ringkasan
Klik Summarize kanggo nyiptakaké rincian AI saka transkripsi iki.
Ngrembugan...
Takon AI Ngendi Transkrip Iki
Ing basa Jawa, tembung iki bisa dijupuk saka tembung = gawé.