so I've created a new IntelliJ project and I've called it threads so go
ahead and do that and of course this is just a basic console application so we
just put in a simple print out....and we run that of
course you've seen something similar million times so far in the course and
we see the output and this is the application running our code on the main
thread and obviously that includes the code in the main method that we would
just to add it to with our system.out.printline
so let's make this a little bit more exciting let's go ahead and create a
thread
so this that we can do this in two ways the first way is to create an instance
of the thread class
let's think about what we want to do first what we try to do here is to kick
off a thread that's going to run some code so we need a way to tell the thread
what code we want to run and we're gonna do that by creating a subclass of the
thread class and then overriding the run method
so rather than creating a thread instance we're going to create an
instance of a subclass so let's create the subclass first so come over here to
our source folder and i right click that and create a new class
i'm going to call that one another thread....
we need to override the run class but before I do that I need to extend from
the thread class so we can access to those methods we type....
....and now we can override we get the additional
options so we're going to select the run method that is the one we want to
override and we're going to remove the super....because what we want to do
is put the code that we want to be executed so in this case we're just going to
print out another console message
.....so now that we've done that let's switch
back to our main class
so after the main thread runs the console we are going to create an instance of the
another thread class based on the class we just created
so to do that we type....
....and when we do
that and run it
you still only see the output from the main class and that's because we have
defined a thread but we haven't invoke the run method yet so we need to
actually invoke the run method so that it execute for us
you might be thinking ok how do we execute this do we used the run method
well in actual fact we use the start method
so....and what that does is it enables to jvm to run
the run method for the thread and if you run that you can see hello from another
thread so clearly the thread has now run and executed the code that we
defined in the run method so again it is important here to remember that we have
to create an instance of the thread which we did on line 8
but then we also have to start the thread independent of that which we did
an on line 9 so lets now print something from the main thread after
we've started the other thread so after line 9
we want to send a message....
.....
so let's run that you can see we've got the output on the screen
remember at this point that when we run we might see the second line from the
main thread printed before the line of the other thread and this is going to
vary so we might have to run a few times just to see that this happens but if we run
it again
running a few more times
we can never assume it's going to run in the same order and it really does depend
on the timings how long a particular thread takes to run
so in this case it's fairly consistent given that it's just outputting but what we
can do here is when we are dealing with threads is keep in mind that it's up to
the system to schedule when the threads will run and in relation to that we can
influence the order of execution by setting a particular threads priority
but even with that said we can never assume that threads are going to run in a
particular order
so keep that in mind that although here it seems to be very consistent with when
running at each time and we're getting the same output each time that isn't
guaranteed because ultimately the decision for the priority in terms of
which threads are running even though we can influence that to a degree by setting
the threads priority
it's up to the system itself to schedule when those threads will ultimately run
now rather than creating a named class for the other thread
we could also have created an anonymous class and this is useful if you only
ever going to want to run the code once in that situation and anonymous
class would work and is probably easy to configure but if you intend to run on the same code
several times and creating a class for the thread like we did with the
another thread class would make more sense and have a better result
but notice here that I said run the code several times and not start the thread
several times things to keep in mind here is we're not allowed to start the
same instance of a thread
more than once so in other words if you create a subclass of thread you want to
execute the code in the run method more than once
then what you have to do is create a new instance of the subclass every time you
want to run it
main method to execute you cannot start a thread instance that is already run in
other words
so let's try starting the instance of another thread again just to show that
it won't work
we should actually see an exception if we try and do that so we'll go ahead and
add the code to do that so we type....
we should see there's an exception when we do that and you can see we have got
an exception on the screen the legal thread state exception
so again using a subclass of thread means that we have to define the run
method once but we can't reuse the same instance
so will delete the second start so we don't see the exception again so let's
do that now delete line 13....
but getting back to anonymous classes when we use an anonymous class we have
to start to the thread immediately
so that's another consideration when deciding whether to use a named or an
anonymous class of course the name class is the one that we already defined
using the another thread class here so let's create another thread as an
anonymous class and have it print a line to the console before the main thread
prints its hello text again or hello again text so come down here after the first
start and then we'll type.....
this is the way to create an anonymous class and it's gonna be public....
.....
....
...so if you run that now you can see that's now
working with the anonymous class and again just a note that the only thing
we know for sure is that the first line we see in the console will be the first
line printed by the main thread
we can't guarantee that execution orders if we try running it again and you can
see what happened here hello from the anonymous class thread that's outputted
after the hello again from the main thread and this is what's happening with
the thread so we've created a thread and we've used .start to run it immediately
but because the thread that runs in the background
the way that the system my computer is scheduled things is that it
outputted the hello again from the main thread before the new thread from the
anonymous class got to execute and that will depend on your computer you may get
a similar result we might get a different result of all together
so basically i'm saying is that may vary from time to time even though it seems
to be pretty consistent with them running here now is likely that on other
computers you'll get different results
and obviously in this case it's a short very trivial example and it's possible
as a result because we're really not doing much in the way of processing in
these threads that really creating that the order is
pretty much gonna be the same each time you run it on your computer
but the point is that may not be the order in which that we created the
threads and indeed that is the case here because in my example we created the
anonymous class on line 11 and yet that code was executed after the system
output was printed out on line 17 and again this may be completely different
when it runs on your computer so we can't guarantee the order that the
threads are going to be executed or in which you know the print statement
ultimately appear here again I'm hammering on this a little bit but I
want to hammer home that we can't guarantee the order in which threads
will execute and that's one of the main reasons why working with threads can be
tricky for you to work with
especially initially to get your head around the concept here so let's now
adapt what we've done to make it a little bit easier to see at a glance
where the output in the consoles are coming from a good way to do that is to
give each it is to give each thread its own color
now the methods are we going to use works with IntelliJ console but it might
not work with another IE console or within the windows native console
because we're going to be using unix xscape code so if you're using something
the other IntelliJ you may not see the coloring but just try and see what
happens anyway but in any event you'll see the colors on the screen this video
anyway i'm going to create a new class here i'm going to call this class thread
color and i'm going to paste in some code now I'm not going to type this code
in because it's really only some constants and you can grab this text
file in the resources section of this video and be able to do the same thing
you did see something similar
in the file input output section in any event so in order to use these cars we
need to append the color code that we want to use before the texts that were
outputting and once we set a color that color will remain in effect until we use
another color escape code
so if you want to return to the default code we use or append rather the ansi_
reset which will set it back to the default color
so let's have the main thread use purple the name one use blue and the anonymous
thread to use red and if you want to you can change it to black but bearing in mind
depending if you're using the dark them or not you might find that it's not
view able so I'm choosing colors that should work on whether you're using the
dark theme or just a regular theme that comes with IntelliJ so
looking at the another thread coloring first click into that and I'm going to
add here.....
hello from another thread you can see that automatically added the import
there for the class we just created
let's go back to our main class again then we're going to add again depending on
whether you're running a white background or the dark background
if your on the white background you might want to do it in black but i'm running the
dark one so I'm going to use purple so ANSI....you can see
that was added automatically and then with the anonymous class we're going to
here we are going to use....
doesn't really matter which one you use so long as you've got a different one
for each of the sections so again we have defined looking back in another
thread we have defined blue for our regular named thread and for the regular
main method were using purple and we are using green for our anonymous class thread
so if we run that we can now see the output with a blue
you might be wondering why I got blue twice there but what I didn't do is down
here
I didn't specify color and by not specifying a color
it's used the color of the last thing that was the output so this was hello
from another thread if we go back to another thread that was in blue
as you can see up there so consequently use that because that was
the next thing that happened to execute in order
so what I should really be doing here is making it....
to guarantee that's going to output it in the right colors so now we should see the
right result
so you can see that we've got now purple showing for our main method the
output from their blue from the other thread the name thread and then a yellow
from the anonymous class read what it's actually green but to me it looks a bit
more yellow than green
the bottom line here is it snow a lot easy to see which statements are coming
from which thread so we don't have to read the thread we can just look at it a
glance and look at the colors and know that that output has come from that
particular thread so change those colors as appropriate if they are a little bit hard to
read so long as you choose a different color for each section you'll be
good to go
ok so I'm going to finish the video here now in the next video we're going to
talk about implementing runnable which is a way to create threads using another
method so the runnable interface is another way of creating a thread which
really requires us to implement
one method but in the event we look at doing that in the next video