Fihàn
0:03
S… Speaker 1 (6- Interrupting a Thread)
Quite often when dealing with long -lived tasks,
0:06
S… Speaker 1 (6- Interrupting a Thread)
we want to give our users the ability to cancel.
0:08
S… Speaker 1 (6- Interrupting a Thread)
So here we are starting a download task.
0:11
S… Speaker 1 (6- Interrupting a Thread)
I want to cancel this after one second.
0:13
S… Speaker 1 (6- Interrupting a Thread)
First,
0:14
S… Speaker 1 (6- Interrupting a Thread)
we go back to our download file task.
0:16
S… Speaker 1 (6- Interrupting a Thread)
Instead of pausing this thread to simulate a long -running task,
0:20
S… Speaker 1 (6- Interrupting a Thread)
let's add a for loop that continuously print a message on the terminal.
0:23
S… Speaker 1 (6- Interrupting a Thread)
So for I equals zero
0:28
S… Speaker 1 (6- Interrupting a Thread)
as long as it's less than integer that max value.
0:31
S… Speaker 1 (6- Interrupting a Thread)
I want a really large number so we can see this task going for a little while.
0:34
S… Speaker 1 (6- Interrupting a Thread)
I++ and here we print downloading
0:39
S… Speaker 1 (6- Interrupting a Thread)
byte i.
0:41
S… Speaker 1 (6- Interrupting a Thread)
With this we can easily verify if our download actually
0:45
S… Speaker 1 (6- Interrupting a Thread)
gets cancelled.
0:46
S… Speaker 2 (6- Interrupting a Thread)
Now,
0:47
S… Speaker 1 (6- Interrupting a Thread)
back to our demo class.
0:48
S… Speaker 1 (6- Interrupting a Thread)
I want to cancel this thread after one second.
0:51
S… Speaker 1 (6- Interrupting a Thread)
So,
0:52
S… Speaker 1 (6- Interrupting a Thread)
let's pause the current thread for one second.
0:54
S… Speaker 1 (6- Interrupting a Thread)
That is thread .sleep 1000.
0:59
S… Speaker 1 (6- Interrupting a Thread)
Just like before we should handle the interrupted exception here
1:03
S… Speaker 1 (6- Interrupting a Thread)
So we're starting a separate thread to download a file and then
1:08
S… Speaker 1 (6- Interrupting a Thread)
have the current thread that is the main thread that is executing this code wait for
1:12
S… Speaker 1 (6- Interrupting a Thread)
one second now to cancel the download thread here
1:16
S… Speaker 1 (6- Interrupting a Thread)
we call thread dot interrupt Let's
1:21
S… Speaker 1 (6- Interrupting a Thread)
run our program So our download
1:25
S… Speaker 1 (6- Interrupting a Thread)
is going and going and going.
1:28
S… Speaker 1 (6- Interrupting a Thread)
How come it's not stopping?
1:30
S… Speaker 1 (6- Interrupting a Thread)
Let me stop the program first.
1:32
S… Speaker 2 (6- Interrupting a Thread)
All right.
1:34
S… Speaker 1 (6- Interrupting a Thread)
The reason our download thread didn't stop is that calling the interrupt method
1:38
S… Speaker 1 (6- Interrupting a Thread)
doesn't actually force the thread to stop.
1:40
S… Speaker 1 (6- Interrupting a Thread)
It simply sends an interrupt request to the thread.
1:43
S… Speaker 1 (6- Interrupting a Thread)
It's up to that thread to decide if it should stop what it's doing.
1:47
S… Speaker 1 (6- Interrupting a Thread)
So back to our download file task class.
1:50
S… Speaker 1 (6- Interrupting a Thread)
Here we should continuously check for an interrupt request.
1:53
S… Speaker 1 (6- Interrupting a Thread)
If you get that request,
1:54
S… Speaker 1 (6- Interrupting a Thread)
then we'll have to stop the download.
1:56
S… Speaker 1 (6- Interrupting a Thread)
So in this for loop,
1:57
S… Speaker 1 (6- Interrupting a Thread)
let's add a code block.
2:01
S… Speaker 1 (6- Interrupting a Thread)
First we check for an interrupt request.
2:03
S… Speaker 1 (6- Interrupting a Thread)
So we type if thread that current
2:07
S… Speaker 1 (6- Interrupting a Thread)
thread we get access to the current thread and then call is Interrupted
2:12
S… Speaker 1 (6- Interrupting a Thread)
if this returns true,
2:14
S… Speaker 1 (6- Interrupting a Thread)
then we can break out of this loop or simply return
2:18
S… Speaker 2 (6- Interrupting a Thread)
Otherwise,
2:19
S… Speaker 1 (6- Interrupting a Thread)
we're going to download the byte now.
2:21
S… Speaker 1 (6- Interrupting a Thread)
Let's run our program again So after
2:26
S… Speaker 1 (6- Interrupting a Thread)
one second our download stopped
2:28
S… Speaker 1 (6- Interrupting a Thread)
So remember the interrupt method doesn't force a threat to stop what it's
2:32
S… Speaker 1 (6- Interrupting a Thread)
doing It simply sends an interrupt request to support interruption
2:36
S… Speaker 1 (6- Interrupting a Thread)
We should constantly check for the interrupted requests and act accordingly Now
2:40
S… Speaker 1 (6- Interrupting a Thread)
if a threat is sleeping and we send an interrupt request to it,
2:43
S… Speaker 1 (6- Interrupting a Thread)
it throws an exception That is why you have
2:48
S… Speaker 1 (6- Interrupting a Thread)
to handle this interrupted exception when pausing a threat

Àkọ́lé yìí láti inú AI (ìdákọ́ àwọn àkọ́lé nípa ìṣàfarawé). O lè ní àwọn àṣìṣe - ṣàyẹwo àwọn àwòrán ìṣàmúlò-ètò fún ìlòòrò. Àwọn ìtàn

❤️ O fẹ́ STT.ai? Fì sọ̀kalẹ̀ fún àwọn ọrẹ̀ rẹ̀!
Àwọn Àkọlé
Tẹ̀ Àwọn Àkọ́gbègbè láti ṣẹ̀dá àwọn àkọ́gbègbè AI tí a kọ́ nípa àkọ́gbègbè yìí.
Àwọn àwọn àkọlé...
Ǹgbàyé AI Àwọn Àkọ́kọ́
Fọ̀rọ̀ ohunkohun nipa àkọ́kọ́ yìí — AI náà yóò bá a lè ri àwọn ààyè tí a fẹ́ ki o si dáwọ́.