In this workbook, we're going to monitor the thread states.
First, we're going to place comments on this scanner related code.
And now from the main thread, we're going to print the state of threat to when it's new.
So here's what I'll say.
Or before the thread starts, we'll have to print.
Threat to get states.
Now, not that.
And if we run the application.
The threat has yet to be created at this point in the runtime, so at this point it is new.
But after we start the thread, if we print the state here.
It's currently running.
OK.
Now, for a task for we want to print the state of thread to when it's terminated, and the requirement
was that we can only sleep the main thread for 10 seconds or 10 milliseconds, I should say.
If I do that thread, not sleep, if I sleep the main thread for a 10 milliseconds and then after sleeping
the thread.
Printing the state of threat to war that give threat to enough time to finish.
I wonder.
Oh, it throws and interrupted exception.
So we're going to have to catch this exception as well.
Catch interrupted exception in case the main thread gets interrupted.
We're going to print the exceptions message.
OK, if we run the code.
And it's still a runnable at this point.
So even though we slept the main thread for 10 milliseconds, Thread two was still running.
Clearly, this task takes a lot more than 10 milliseconds to process.
So what we need to do is interrupt the thread right here.
We're going to say thread to interrupt.
And as soon as we interrupt, thread to.
We need to stop this function from performing its task, so we'll say if the current thread that's running
this code, if it's interrupted, then just return right away and just, you know, return zero.
What's wrong one?
And now it should definitely not take more than 10 milliseconds for this to run.
So the tasks should be relatively short.
Nice.
And at this point, it's runnable, and at this point it's terminated.
Perfect.
OK, now in preparation for part three, what I want you to do is Command Z Mac or Control Z Windows.
Everything we did for this workbook.
This was only for knowledge purposes we're not going to be going through with it in the remaining exercises.
So until your scanner code gets on, commented.
Keep reversing your changes and voila.