so far the threads we've created don't use any variables all they do currently
is print text to the console
so let's create a couple of threads to do something a little more interesting
so we'll start by creating a new console project clicking on create new project
as we've done probably hundreds of times now selecting java next and creating
command line app and i'm going to call this one counter
....ok
and moves this a little bit so we got a bit more space for our source
code
so what we're going to do is have two threads counting down from ten using a
count down object so will add two more classes to our application count down
and count down thread
now we could put them in their own files but since this is just a simple demo
we're going to add them to the main java file
so let's write the code to do that and then we'll discuss it as we work through
it
first we're going to copy the thread color class we used in the last lecture
in this project so i'm just going to copy that and i'm going to create the
new class and if you recall it was called thread color....and i'm
just going to paste that code in save a bit of typing
like so so it's just the very basic codes that we used in the previous
lecture so let's now go back to our main class and in our main method will put some
code in it so we'll come down actually what we will do first is will create
these classes and i mentioned we're just going to do that because this is a
simple demo we're going to create these classes in main.java
so the first one is going to be....
....
.....and we need string for the color....
....
....and if you get this error here what that means is we haven't
got jdk 8 set up so we need to go back into preferences
sorry into the project right click the project and click on open module
settings
we need to make sure under project that our project language level i think
I said JDK but project language level is set to 8
and if we do that the error then goes away because we're able to use a string in a
switch statement which is only introduced in a later version then our default
which was set to 6
so the first one here now is we're going to do....
....and we're going to be having two threads running so
that's the case we're going to change the colors....
....
...
....and we'll break out of that....
....
....and the default is going to be....
....
ok so all the it has that done is set the color
so now we want to do some output so put something on the screen so we put....
....
...so we're counting down from ten
I'm going to print out of the color which sets the color when we print it out
....and we need to add the number as well so
it's going to be....
....so that should be all we need for
count down now we're going to create a second class and this class will be will
extend the threads so its going to be...
....
...
....
....then we want a constructor so...
....
....and all of the\constructor will do is assign a value to
our fields so...
and then we need a run method so....
...which is executing that method that we
created on line 13 then the other thing we'll do is we'll come back here to the
main method now that we've created those two classes and we'll put some
code in here to set the names and to s the threads to do that will create
a new object first.....
then we'll create the two threads so....
....
...once we've created that lets assign the name so...
....bearing in mind that these need to be identical to what we
have defined on line 21 and 24 below rather and the second one....
....
and all that's left for us to do now is to execute it so we want to start to be
more correct so...
ok so that's the code....
....
so let's just go through what we typed in so looking at the count
down class first which we've got to defined on line 18 is one method there the
do count down method and this method is called the code counts down from ten
to one
at which point the for loop then exits because it's obviously finished
we're using the color trick that we've used in previous videos and we can see
from the switch statement that we've got one thread named thread one which will
print the text in cyan and second thread called thread 2 which is going to print
the text in purple and we also have count down thread class that we created on
line 39 and extends thread obviously because we need to do that so that we
can execute it we can start it
so when an instance of that classes is started the run method will call to do
count down method in the count down class
so all instances of this thread will count down from ten to one
so we create the threads in the main method as you saw at the top here we
gave each of them a name and pass the same count down object to both threads and
lastly we start the threads so lets now run this and see what happens
so you can see what happen there there was a thread one running which
count down from ten to one in cyan and thread 2 did the same thing but in
purple and you can see nearby looking at this how the system switching between the
two threads so we can't predict when each thread is run and will be
suspended so every time we run we going to see a different result here so let's
run a few times just to see what I mean so if we run this again noting the order
that was showing there you can see that one was entirely different we actually
got the complete results in cyan for thread one running it again
and there's another results sometimes it's the same but as you can see by if I keep
running it it does change and we can see we've got purple their the thread 2
coming up a couple of times so we really can't guarantee the order or can't
predict when each thread will run and when it will be suspended so that the other
thread can run because that's based on the operating system and what it's doing
so now that we've done that let's make a small bit of change to the code instead
of using a local variable in the for loop
let's use an instance variable so to see what I mean going to come back up here to our
count down method we're going to introduce an instance variable or field for this
class so....and then for the for loop instead
of creating the variable we're going to use the instance variable instead i'm
going to get rid of the initial definition and just put....
which is now going to obviously use the instance variable so all we've really done
here has changed the local variable to an instance variable so you think that the
code should still count down from ten to one and if we run it
we should expect the same thing will probably you would expect the same thing
each thread counting down from ten to one but let's just run the application
to confirm that
and notice how we've got something different this time
we didn't see what we expected to see instead of each thread counting down to
10 both threads seemingly
have seemed to skip numbers
so it's possible that some numbers here are even duplicated and you can see that
in the case of 10 thread 2 and thread 1
the numbers are duplicated and since every time we run the application we get
a different result
we're going to discuss the following results that i got for one run of this
application
so here's an example image just to make it easy so that we're all on the
same page you can see their so we can see in
this particular example the number 10 is duplicated and we got that when we run
this live as well but after that in this particular example each number only
appears once so thread 2 Skip's all the numbers that thread one prints and vice
versa and also interestingly enough thread 1 prints 3 after thread 2
prints 2 so what's going on here so
I mean after all we did was change the local variable to a private variable
other than that we didn't really touch the code but as you can see the results
have now changed drastically
compared to when we originally ran it in both threads were happily printing out
the full numbers
so let's talk about what's going on and gain an understanding of that in the
next video