going to start this section on java
currency by defining a few key terms that you need to know as they relate to
java so we are gonna start of using the term process and that's a unit of execution that has
got its own memory space now each instance of a Java Virtual Machine JVM runs as a
process I guess that's not strictly true for all JVM implementations but really
for most of them and when we run a Java console application which we have been doing a lot of
this course what we're doing is we're kicking off a process when we run a Java
FX application we're kicking also off a process as well
now many people use the terms process and application interchangeably and
we will in fact had done that in this course will do that in this section as well
so if one Java application is running and we run another one each
application's got its own memory space also known as the heap so the first Java
application can't access the heap that belongs to the second Java application in
other words that heap isn't shared between two applications or two processes they
each had their own lets talked about a thread so a thread is a unit of
execution within a process so each process can have multiple threads and in
Java every process or application has at least one thread and that's the main
thread for UI applications is just called a JavaFX application thread in
fact just about every Java process also has multiple system threads that
handle everyday tasks like memory management and input-output we as the
developers of Java applications don't explicitly create and code those
threads our code runs on the main thread which is created automatically by your
Java program or in other threads that we explicitly create and in this
section of the course you are going to be learning how to create these threads so creating a
thread doesn't require as many resources as creating a process and a thing
to remember is that every thread created by a process shares that processes memory
and files and this can cause big problems with your applications and you will
find about that in a later lecture
now in addition to the processes memory or heap each threads got what is called
a thread stack and the thread stack is memory that only that particular thread
can access and we are gonna look at what stored in the heap vs what's stored in
the thread stack in a later lecture in this section as well so
every Java application runs a single process and each process can have
multiple threads within so every processes has a heap and every thread has a thread
stack I guess at this point you might be asking why would we want to use
multiple threads in our application and why not just stick with the main thread as
we've been using you know in the course what are the advantages
well theirs two main reasons why you wanna do this
firstly we sometimes need to perform a task that is going to take a long time now for
example we might want to query a database or fetch a data from a website on the internet so we
could do this on the main thread but the code within each thread executes in a
linear fashion so the main thread wouldn't be able to do anything else
while its waiting for that data to come back from the data base or from the
internet
another way of putting this is that the execution of the main thread would be
suspended so has to wait for the data to be returned before it can execute the
next line of code and to the user this could appear as if the application is
died or frozen especially when we're dealing with a UI application and if you have only been through
the JavaFX section of this course I did show you a simulation of locking up a UI
application where it appeared to be frozen waiting for a particular thread
to finish so these are big problems that we need to avoid as developers so
instead of tying up the main thread what we can do is create another thread
and execute the long
running task on that thread that we just created and what happens there is that
frees up the main thread so that we can continue working and executing and it can report
progress or accept user input and perform other tasks on the screen or on
other part of the program while that
long running tasks that we kicked off in another thread continues to execute in
the background that's the first reason you might want to do it but the second
reason we might want to use threads is because an API requires us to provide
one now sometimes we have to provide the code that will run when a method that we have
called reaches a certain point in its execution so in this particular case we
usually don't create a thread
we pass in a code that we want to run on the thread and we will see examples of this later
in this section as well and thats bring us to concurrency which refers to
an application doing more than one thing at a time now that doesn't necessarily mean
that the application is doing more than one thing at a time or at the same time what
it means is that progress can be made on more than one task so let's say that an
application wants to download data and draw a shape on the screen
so if its a concurrent applications what it can do is it can download a bit of
data then switched to drawing part to shape and switch back to downloading
some more data then switch back to drawing more of the shape and so on
so what concurrency really means is that one task doesn't have to complete
before another one can start and a good thing here is that java provides thread
related classes so that we can create java
concurrent applications so that is a very short explanation of some of the
terminology but don't worry if your having trouble grasping this concepts
concurrency is something that's best learned by examples we move on in future
videos to creating some threads so you can see how this works by analyzing the Java code
itself but before we do we really have to understand that when working with
threads were at the mercy of the JVM Java virtual machine in the operating
system when it comes to when the threads are scheduled to run and as a result or
because of that output can vary from run to run and you might not see the exact same
output that someone else's see when they run an application on their computer
that uses concurrency as an example we can't guarantee in which all the lines we
printed to a console because of the very nature of threading itself but that's it in a
nutshell
lets start in the next video going through some practical examples of how threads
work see you in the next video