One thread can interrupt another thread.
In this lesson, you will interrupt a threat to from the main threat.
Why would you interrupt a thread?
Well, if something goes wrong in one thread, it might not be worth continuing the other thread.
So the first thread can interrupt the other thread.
When you call thread, don't interrupt the thread status becomes interrupted.
So right after starting the thread, what I'm going to do is.
I'm going to print.
Oh, no.
A big error occurred.
In the main thread.
Let's interrupt the background threat.
And then here, what I'll say is thread to interrupt.
OK, run the code.
And everything proceeds as normal.
But where shouldn't the threat have been interrupted as soon as we started it?
Why did it keep going?
Well, going back to this slide, all that happened is that the status of our threat became interrupted.
That's it.
But what we can do is use that interrupted status to our advantage.
So right after we interrupt the thread, we can check the interrupted status.
Here I can print.
Threat to boycott is interrupted, so this basically returns a Boolean that reflects the status of a
threat.
If we print it.
The status is that it is interrupted.
That is true.
So let me just honestly delete all of these print statements there, but in the way.
So that's perfect, so the interrupted status of threat two is true, but that's kind of weird, so
we're making progress on both threads.
Eventually, we get to this line, which supposedly interrupts the second threat, but all it does is
it changes its status and our threat to keeps getting processed with an interrupted status.
What we need to do is actually interrupt threat to as soon as its status becomes interrupted.
So here, what I'm going to say is elseif.
And here what I want to do is check if the current thread that's performing this task thread, that
current thread, if it's interrupted status is true.
Then I'll print.
This task has been interrupted.
Prematurely.
And break the one loop.
We were running our coat.
And that's perfect as soon as our threat starts.
It gets interrupted right away.
If I were to put break points here, here and here.
And debug the runtime.
And as soon as we start our background thread, we see two points of runtime.
That's because our application is making progress on two threads at a time.
And it seems that as soon as we started our second thread, something bad happened in Maine and were
interrupting the background thread to right away.
As soon as we interrupt the background thread, its status should change to interrupted.
And it seems like for me, at least, the main thread keeps running and now the main threat is finished
executing.
We're exiting the main thread.
OK.
But the background threat is still running.
And because it's interrupted state is true, we're breaking the wire loop.
And here we are exiting the second thread.
And by the way, just because my code ran this way doesn't mean your code is going to run the exact
same way.
The task scheduler is extremely random and it's hard to predict how it's going to organize your tasks.
So don't expect the exact same runtime as me.
Concurrent execution is random.
Thread that sleep stops a thread for x milliseconds.
And it throws an interrupted exception when it's interrupted.
Inside the long task, I'm going to say thread sleep.
For 3000 milliseconds, so whatever thread that's currently running this long task, it's going to sleep
for three seconds and after it's done sleeping, we're going to print.
Done sleeping.
And notice that it forces us to try to run the code because this code throws an interrupted exception
when it's interrupted.
And we're going to catch the exception if the code fails or in this case, if the threat gets interrupted,
it's not really a failure.
And then I'm going to comment all of this out.
And now if you rerun the app.
Our application never gets the chance to sleep.
Why do you think that is?
Here's a visual of the runtime, we're making progress on both threads at the same time as threat to
is sleeping.
Eventually, Main interrupts that thread.
The sleep method throws an interrupted exception and our thread finishes.
Now, if we remove the interruption.
Our main thread finishes right away, but our background throughout is still sleeping, and now it's
done.
Here's a visual of the runtime you just saw.
Our main thread pretty much finished right away while our background thread was sleeping for three seconds.
One thread can interrupt another thread.
If something goes wrong in one thread, it might not be worth continuing the other thread.