This video introduces the fundamental concepts of multitasking and multi-threading, explaining their historical development and the benefits they offer in modern computing, particularly within the context of Java.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
in this video I will give you a quick
overview of Java multi-threading and
concurrency but before I do so I will
just give you a quick overview of the
history of multitasking and
multi-threading in general just a quick
heads up this video is an introduction
to Java concurrency and multi-threading
on a conceptual level therefore it does
not contain any code examples however
this video is part of a playlist of
videos about Java concurrency and later
videos in this playlist do contain code
examples if you check out the
description below the video you will
find a link to this playlist as well as
to my textual Java concurrency tutorials
which also contain code examples in the
early days of computing a computer could
only run one program or process as
programs also sometimes referred to at a
time only one at a time right so that in
that case if you were working inside of
word or something similar like that and
you needed to make some changes to
something inside Excel you would have to
close down word and jump into Excel and
change the thing you needed to change
and then close Excel again and an open
word up again and then that kind of
workflow quickly becomes quite annoying
for the user to solve this problem
multitasking was invented and
multitasking basically means that now
the computers can run more than one
application at the same time and but
since the CPU can only execute one
program at a time the CPU or the
computer solves this problem by
executing one application for a little
bit of time then switching to another
application and executing that for a
little bit of time then switching to
another application and executing that
for a little bit of time then switching
back to the first one and repeating this
way it will appear to the user as if all
of these application our applications
are actually running at the same time
even though they are actually only
running one at a time but the switches
are so fast that the user cannot see or
feel the difference it feels as if
the applications are running at the same
time I said that it was the CPU that is
switching between these applications
here these tasks but in reality it is a
combination of the CPU and their
operating system right so the CPU
executes the operating system and the
operating system then switches between
the tasks here and modern-day CPUs have
some built-in features that makes this
task switching easier for the operating
system multi-threading is the same
principle as multitasking except
multi-threading happens within an
application so instead of having one
thread of execution inside of an
application you can actually have
multiple threads of execution inside of
an application so for instance an
application might both be downloading a
file in one thread and playing music in
another and the way that threats are
executed is similar to how tasks our
applications are executed first one
thread here gets to execute a little bit
on the CPU and then the operating system
or the application depending on who
controls this thread switches to another
thread and now this thread gets to
execute a little bit and now we switch
completely the operating system switches
completely to another application and
then that first thread here inside of
this application gets to execute a
little bit on the CPU and then it
switches to another thread and then this
thread gets to execute a little bit on
the CPU and then we switch completely
out of this application and back to the
first one here or to any other
application running on the computer
modern computers typically come with
multiple CPUs in or CPUs with multiple
cores in which in practice is the same
thing so on a modern computer you might
actually have multiple CPUs executing
these applications
and in that case that means that some of
the applications might actually be
running at the exact same time because
they're being executed by different CPUs
for instance here this application here
might be running at the exact same time
as this application down here because
they are executed by different CPUs now
each CPU may still context switch
between applications and internally
between threads within each application
so some of these applications and
threats are not exactly executing at the
same time but some of them will be
because of the multiple CPUs right by
the way in this diagram it looks as if
each application is only executed by a
single CPU but that is not necessarily
the case it is possible to have an
application running which has one thread
executed by one CPU and another thread
executed by another CPU down here for
instance and in that case each of the
threads might actually be executing at
the exact same time because they are
running on different CPUs and this is
often the case in software that requires
a lot of compute power for instance 3d
rendering software video editing
software when the final video has to be
exported or computer games now that we
have discussed what multi-threading is
let's dive deeper into why
multi-threading is a good idea first of
all we can achieve better CPU
utilization with multi-threading take a
look at this example here I have an
application which has one thread running
here and at some point here the thread
needs to load some data from disk offer
network now while this data is loading
from the disk the CPU cannot really do
anything within this threat of execution
up here it needs the data before it can
continue but instead of simply being
idle and wasting these CPU cycles the
computer can switch to another threat and
and
execute whatever work this threat needs
to get done
and at some point when the CPU or they'd
like the operating system feels that it
is time to switch back or like when the
full data here is loaded it can switch
back from this threat down here to the
first threat which can now continue
running because now that data has been
loaded into memory in this way we can
better utilize the CPU while IO tasks
are running another benefit we can
achieve what multi-threading is better
io utilization look at this example here
first we have a thread running within an
application and now this threat needs to
load some data from the disk and then
it's been some time processing these
dates and after that it loads the next
data and then it processes that data etc
now as you can see we are actually
switching between full utilization of
the CPU and full utilization of the
network i/o but we have these gaps in
between here and here and here and here
where we are not using either and
imagine then that we have another thread
it's not the same thread but another
thread running which you can see here
this thread up here is idle right in the
CPU is idle up here while it's loading
data from the network so we can have
another thread here which has asked for
some data which it can now process and
then it this one can now load data while
this thread up here is not loading any
data from the disk or network right it's
actually utilizing this empty space here
this idle I opened with is being
utilized by another thread and so and
thus it can switch between these two
threads that are utilizing the i/o at
different times so when one is waiting
for IO the other one is using the CPU
and when the other one is using the CPU
the other one can use
I owe and you know the discarded network
and actually you might even have two
threads using the for instance the
network cut at the same time for
instance if they are both downloading a
file or waiting for a file to download
from a server which is slow you might
actually be able to to start multiple
i/o downloads at the same time from
different threads and so in this way we
can not only better utilize the CPU but
also the i/o capabilities of the
computer that bandwidth to the disk and
to the network a third benefit we can
get from multi-threading is higher
application responsiveness as perceived
by the user
imagine you have a GUI application a
graphical user interface the user clicks
on a button here it starts some some
some processing here and then it starts
a long-running task which runs in the
background and when the task is finished
the thread can go back to updating the
user interface and responding to further
input from the user now in this period
here the threat is busy executing the
long-running task and thus it cannot
respond to the users input or update the
user interface and that means that in
this time period here the application
will appear unresponsive to the user now
to avoid that we can start the
long-running task in a separate thread
down here right and thus the CPU will
switch forth and back between the
long-running task in the separate thread
and the main UI thread in the UI thread
and so it switches forth and back and to
the user it appears as if the
application is still responsive because
at all these times here the application
still responds to the users input but
sooner or later because this application
will probably not use all of the CPU
time sooner or later enough CPU time
will have been a look
to the long-running task in the separate
threat down here and so it finishes and
then the user interface can be updated
saying oh by the way this file is now
fully downloaded or whatever else this
long-running task in the background is
in a computer that contains multiple
CPUs the long-running task here might
actually be running on its own CPU which
is a different CPU than the one
executing the main UI thread and thus we
might start the action here like this is
just a little bit of initialization then
the long-running task starts and because
this task is running on a different CPU
this thread is running in a different on
a different CPU the main UI thread can
continued undisturbed on the other CPU
or the its own CPU which is executing
this thread up here so now the
long-running task is actually executing
at the exact same time as the main UI
thread is continuing to respond to input
from the user
as you can see multi-threading is really
beneficial to us in computer programming
however it is not without its problems
without its issues that we need to learn
how to handle first of all the the first
model that appeared for concurrency was
this yet mutable state model in which
threads could read and write the same
memory inside of the application however
this type of concurrency model leads to
possible race conditions invisible
rights and when you try to fix those two
you can get congestion
deadlock nested monitor lockout
starvation slip signals miss signals and
a lot of other things to solve these
problems either you can solve them with
some constructs that help you handle
these problems or you can completely
change the concurrency models who are no
shared mutable state concurrency model
and exams of such models are separate
state concurrency where the threads are
only communicating with
with messages they don't share any
actual data inside the application um
they don't share access to any writable
state in the application you have
functional parallelism and parallel
pipelines etc I will not get into
further detail about either these
problems are these concurrency models in
this video but if you check out the
description below the video you will
find a link to the playlist my Java
concurrency playlist and the videos in
that playlist will explore some of these
concepts in more detail and additionally
if you find if you check out the
description below the video you will
also find links to my textual versions
of these tutorials which will cover many
of the same topics as the videos but
sometimes the texts are a little bit
ahead of the videos that's all for this
introduction to Java concurrency and
multi-threading if you liked the video
hit the like button and don't forget to
check out the description below the
video for a link to the full playlist
with all the videos about Java
concurrency and if you want to see more
videos like this so why not subscribe to
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.