ok so we're talking about getting another thread to interrupt
another thread and we talked about using the interrupt method to do that so means
that we need to have a reference to the thread instance and the easiest way that
we can test this is to go back to our main class and interrupt the another
thread class because we've got a reference to the another thread instance
their so let's go back and do that and what will do is so will call the
interrupt method just before printing the second line from the main thread
so just after the my runnable thread and .start come down here and we will put another thread...
....and then on line 32 we got our regular second
print out from the main thread but let's also go back and change the run method
in another thread because we wanted to terminate if it does get interrupted and
to do that
it's quite simple all we really need to do is return from the run method so we
wanted do a return straight after we printed the message to say another
thread wake me up so we add the return there which would immediately terminate
the another thread instance let's go back to the main method and run this and
see what happens
and you can see that it finished quickly and we got the message here and
another thread woke me up
and of course that message came from the interrupted exception that was thrown the
message their on line 17 and that came about because we're used another thread
.interrupt on line 30 and just to confirm let's delete that line again now
and just check that it still does do the interrupt correctly so run again
now we should find that it works as it did before pause their for three seconds three
seconds have passed and we see the regular output because this time it
didn't get interrupted and it didn't terminate at least up until the point of
printing that second message then of course it terminates because it's
finished the run method
so we see that we can put a thread to sleep and it's possible to get it to
wake up periodically to see if there's any work for the thread to do so let's
say we've got a situation where we know that a thread can't continue to execute
until another thread is terminated for example we might know that there won't
be any data to process until the thread that's fetching the data
has completed executing so in that scenario rather than wake up the
thread periodically to see if there's any data we can join the thread to the
thread that's fetching the data and when we join a thread to a second thread
what happens is the first ever thread will wait for the second threat to terminate and
then it will continue to execute
so let's join the thread that we assigned to the my runnable thread
variable in the main class to the another thread instance
so the thread will then stop executing until another thread is terminated at
which point is gonna wake up and continue executing now the code that runs
on the my runnable threads contained in an anonymous runnable object and that's
perfectly okay
it doesn't matter whether we join from a thread instance we created using a
runnable or a thread instance we created using a subclass of the thread class
all we really need is a reference to the thread that ultimately want to join with
so if you come back here and in the my runnable thread and after the first
print out
we need to add a try-catch so....and as we're going to type in here
another thread.....and we need to catch interrupted exception again.....
....i'm going to print out in the same color for this
particular thread which ANSI red....
....
and of course that will only happen if indeed it was interrupted for some
reason
so again what we're doing here is we are calling the join method of the thread
that we want to join the current thread to
in this case we want to join to another thread so we called a joint method using
the another thread instance and you saw me do that on line 27 and will notice that
again as I mentioned join can throw the interrupted exception and that's because
just like with the sleep method join can terminate prematurely
if this thread is interrupted by another thread so when working with threads we
already know we can't guarantee in which all the threads will execute
we also can't make assumptions about how long threads will sleep or wait
so really what we're getting at here what I'm getting at here is really never
any guarantees
now when we run this code which we will about to shortly another thread will
put itself to sleep because of that we should see the code running in the my
runnable instance print hello
then we hear it from again until another threads woken up we
should be around three seconds
at that point the my runnable thread is going to wake up and continue
execution and we'll see that it will print that its running again
of course what I need to do is to print that message....
...so i'm going to do that...
....
....so again what should happen is when we run the main method another thread going
to pull itself to sleep because of that will see the code running on the my
runnable thread instance print hello the first part before the try then
we wait here from again until another thread is woken up
we should be around three seconds at that point then hopefully the my runnable
thread
wake up and continue execution then we'll see that print that i've just
added on line 28 showing on the screen
so let's run this now and we can see that that did work you can see that
we have obviously the 3 seconds where the another thread was sleeping
and we also had the my runnable thread printed out the first message hello from
the anonymous classes implementation it then pause and you notice that didn't
then continue and show the second message on line 28 up until we had seen
that the another threads three seconds have elapsed
so in other words it did correctly wait for that to terminate before it finished
itself off and obviously this is quite a simple scenario so we didn't really have
any problems running this because we're not interrupting any of the threads
what would happen and if we go join thread a to thread b and thread b
never terminates
well what happened is our application wouldn't terminate and it would probably
look as if it had actually died or crashed so to prevent this
situation we can pass a timeout value to the joint method and you may have seen
when I was typing in this method here this method here on line 27 there was an
overloaded method their
so when you add a time out value thread a is gonna wait for thread b to
terminate or for the timeout period whichever happens first so in other
words if the thread b has been terminated when the time out periods
has pass and thread a will stop waiting for that to happen so let's try
this when we get back and we'll put in another thread to sleep for five seconds
so go back and make that change
so make it five thousand milliseconds so 5 seconds then go back to main
and in the join method on line 27 would use the overloaded method and we'll get
it to wait for two seconds and it uses a millisecond just as the sleep method does
so again 2000 being two seconds and just to be consistent if I
close the run window and probably wouldn't hurt to put here another thread
terminated or timed out , start running again
so if we do that we'll run that we'll see what happens and we saw that the
another thread terminated or timed out and so it finished early and then we
actually changed the text for this at five seconds has pass and I'm awake
remember we change that to five seconds in the another thread if we
go back there again now and change it back to 3,000 and go ahead and remove
the parameter from join again
we should find it operates like it did before 3 seconds have pass and i'm awake and
another threads terminated or timed out which is what we expect to happen
so obviously when we join one thread to another thread and the join
times out
we have to handle that case in a real world application for example if the
thread was waiting for data and the join times out then we wouldn't have
any data to process and we have to somehow report that to the user you know error
downloading or something like that
now there are other methods in the thread class we can use but we've
covered the ones that we will use the most
the only other one i want to mention is the set priority method and we can use
that to try to influence the order in which threads are run but they should
really regard setting a threads priority is more of a suggestion to the
jvm in the operating system rather than a command
in fact some operating systems don't support this set priority at all and
will ignore it as usual never assume that setting priorities will force threads to
run in a particular order it's really ultimately up to the jvm and the
operating system is to how that runs and just out of interest to learn more
about the set priority method and the other methods available in the thread
class
check out the documentation and I'm just going to bring up on the screen so you
can have a look
....and there's the link on the screen
so do check that out to find out more about some of these other methods
and also about the set priority method quite useful information there for you to find
a little bit more about to the thread class
ok so that ends our introduction to threads and concurrency in the next
video moving forward we're going to look at how working with threads can lead to
nasty bugs that can be very difficult to debug and ultimately show you how to get
around those as well so see you in the next video