we've already learned a lot about thread second with concurrency and we've seen how easy
it is for an application to experience race conditions or thread interference
when multiple threads are running so to help developers write code that involves
multiple threads the java developers introduce the java.util.
concurrent package in Java 1.5 along with several other sub packages
now these classes and interfaces in this package tree
are there to help developers properly synchronized code and that also make it
easier to work with multiple threads as well
there are a large number of classes in this package and we're going to
touch on a few of them
so to start with we're going to create a new project going to call that one
producer consumer....
ok obviously it's a console application and i'm going to create a new class the
class will be called thread color and that will be the same thread color code
that we've used previously that will be available in the resources section of
this video and i'm just going to paste in those code definitions which are
unchanged
so in this application their will be a thread that adds data to a buffer
in other words the producer as well as two threads that can read data from the
buffer the consumers
so let's start by adding the code for the producer class and as we've done
previously
we're just going to add this class to the main java files so go back to that
one
just add the classes down to the bottom so start off by calling this class that
we're creating producer....
....by the way i'm
calling it my producer because if we try just call it producer Intellij
will helpfully or
perhaps not so hopefully in this case trying to import from one of the many
producer classes that are already in the jdk so we just keep this one simple by
calling it my producer and we're implementing runnable because we want to
run it on a background thread so let's start by creating space for our buffer...
....
and will call it buffer.....
....
....we are going to accept the two parameters of the product buffer and
the color.....
and let's implement our run method as well so....
....
...
....
...
....
...
....and then we need to catch that
exception so...
....
....so the constructor
accepts the buffer
that's going to be shared by the consumers and also the color that will
be used when it prints to the console and in the run method you saw that the
producer will write the numbers one to five as a buffer or to the buffer as
strings before writing into the buffer it separates a message to the console with
the right color so we can see what it's doing and after running a number go
to sleep for up to one second
using the random class to generate that interval and when it's finished writing
the numbers we going to write an eof which is an end of file
string to the buffer to let the consumers know that there won't be any
more data coming or is no more data to process and we'll talk about the value of
eof in a minute
ok so the next class - right now is
unsurprisingly the consumer class let's go ahead and write that....
....
...
....
this is going to be a little bit different because we will be processing
the buffer instead of effectively creating it so it's going to be...
....
we're going to continue and we will be talking about it shortly....
...and I'm going to come back to eof shortly
we're going to print a message color....so if we found our f and we defined
that shortly because we're not going to accept that default import
so we have found that then were going to break out of a loop effectively
otherwise if it wasn't any eof then we're going to put in a message
....so you can see there that the my
consumer class also implements runnable so that it can run its code on the
thread and accepts the buffer shared from the producer and the color its going
to use when it prints to the console when the run method the consumer will
loop until it reads the eof the end of file string from the buffer which
we will define shortly
inside the loop we first check to see if there's anything to read you know if
there isn't
we loop back around and check again and then keep checking until the buffer
isn't empty when that's the case it then looks at the first element in the buffer
to see if it's that end of file the eof if it is it prints that its exiting
to the console and then breaks out of the loop
it doesn't remove the eof string because if it did that other consumer threads
would loop indefinitely so that's an important point there
if eof is removed the condition that checks for eof would never be true for
the other consumer threads and we wouldn't want that so that's why were
specifically exiting without removing that particular entry that last entry
so therefore consumer threads have to leave eof from the buffer which
is fine because the producer is indicated that there won't be any more
data at that point anyway so if the first element is an eof it reads from
the buffer and print it to the console and then goes back to the beginning of
the loop now in a real world application we wouldn't loop like that we would have
the consumer sleep for a while before checking the buffer again or we have a
system that manages threads and uses the waitp and notify all methods that we
looked at in the previous videos
keep in mind that this example is a contrived one and we're keeping things
simple
so that we can focus on what we're learning in this lecture but that's the
gist of what we're trying to do in them in the my consumer class at this point now
let's add the code for the main method and that eof constant that's sitting
there so go back up to our main class
let's add it firstly the eof that's going to be public...
...and down here
we can accept the import which was the import for the one we just created
...ok then going back up to our code in our main method class
going to start with a creating a buffer so it's going to be list....
....
...
....
.....
....
then all that's left for us to do now is just to start the three threads so its going to
be...
....and incidentally notices on line 7 this
static import that's there so that the my consumer and my producer classes
can use it without having to qualify it with the package in class name and
obviously in your case you're import will match whatever package name you're using
and eof is of course the string constant that we defined
so let's finish this video here now in the next one will continue on and we'll
start running this and see what happens
see you in the next video