This tutorial introduces the fundamental concepts of multi-threading in Java, focusing on the two primary methods for creating and starting new threads: extending the Thread class and implementing the Runnable interface.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
hello this is John from Cave of
programming. comom and this is the first
in a series of tutorials on
multi-threading in Java so in this
tutorial we're going to look at starting
threads in Java and next time we're
going to move on to looking at basic thread
thread
synchronization so I'm here in eclipse
and I'm going to create a new project
let's create a new Java project and I'll
one and I'll give that a um a main
class uh which I'll put in a package
called demo one so let's call this just
app and I'll put it in the demo one
package now uh there are two basic ways
of starting a thread in Java and a
thread is just kind of like a separate
operating system process which can run
concurrently with other
threads and uh the first way of starting
a thread in Java is to extend the thread
class so let's say here class um I'll
call it Runner um extends
thread now the thread class has a method
called um run and I want to override
that method and put some code in there
so I'm going to right click here go to
source override Implement methods and of
course I could also do this by hand and
the signature is just public void
run so I'll override that and get rid of
this unnecessary stuff here now in run
I'm going to put some code that I want
to run in its own separate
thread and um I'm just going to put some
code in here to simulate doing some
useful work so let's have a loop I'll
say for in I equal not I less than
i++ and I'll just output here some text
with a system out I'll say sis CIS out
control space to to complete and I'll say
say
hello and let's just add on to the the
loop index now to make this a more
realistic simulation I'll slow this Loop
down a bit and I'll put in here
I'll put a I'll use a static method of
the thread class called
sleep and um sleep pauses your program
for the specified number of milliseconds
so let's just pause this for 100
milliseconds which is 1/10th of a second
and um annoyingly thread. sleep throws
an interrupted exception or can do so I
need to just handle that exception here
work so to to actually use this class
and to actually run this code in its own
thread I need to declare an instance of
my class down
here I'll just call this Runner one and
I'll set that equal to a new
Runner and to run this code I need to
type Runner one um do start so I'm
calling the start menu of the thread
class um it's important not to call run
because if I call run it certainly will
run my code but it's going to run it in
the main thread of the application if I
call start that tells the thread class
to go and look for the run method and to
run that in its own special thread so
that's why I'm calling start
here and if I run that now um I'll see
some code and it's just running and it's
actually running in its own thread and
to prove that um let's just copy this
and I'll have another Runner Runner
two and I'll run them both at the same
time so if I run this now and you can
see we've got here um interleaved output
so let's scroll to the top I've got 0 0
1 1 22 and so on so both Loops are
running concurrently they're running at
the same time and that's kind of the
whole point of threads that you can run code
code simultaneously
simultaneously
so that's the first basic method of
starting a thread in Java the second
method is to implement runable and pass
it to the Constructor of the thread
class so I'll create a new main program
here and its own package to demonstrate
this I'll call it app again and I'll put
it put it in a package called demo [Music]
[Music]
2 and um I'm going to create a new class
up here class class I'll call it Runner
again and I'm going to say that Runner implements
implements
runnable and runnable is an interface
which just has one method in it public
void run so let's um let's just click
this eror here and go to add
unimplemented methods and now I've got
my public forid run method there and
just as before so this is the last slot
of code um I'm going to put my code that
want to run in the public void run
method so I'll just copy
that and I'll paste in to the run method
of my class that implements the runnable
interface and now to run that I'm going
to declare an instance of the thread
class I'll call it T1 and I'll set that
equal to a new
thread and I'll pass an instance of my
Runner class here to the Constructor and
the quickest way to do that is just to
say say new
Runner and to show that I can get code
uh running at the same time I'm going to
have two of
those let's just copy
that and I'll have
T2 and now to actually make the code run
I need to call t1. start and of course t2.
t2.
start and if I run that now we're going
to see the same thing again down here
let's just maximize the set and run it
um so I've got inter leave output once
again and both um threads of Co are
running at the same
time now sometimes um you just want to
run like one method in its own thread
and it seems like a lot of hassle to
create a whole separate class and uh
there is just like a quick way that we
can do um either of these um things
really and I'll just demonstrate that
here so this isn't a new way of um
running a thread it's just um a
technique using an anonymous class that
comes in handy sometimes and let's put
this in a package called demo
3 and um get rid of this stuff so to run
just a little bit of code you can do
something like this I'll say
thread and um here
in inside the Constructor brackets of
the thread class I can say new runnable
round brackets just as I I was trying to
create an instance of a class called
runnable which I'm not um but if I open
a bracket here an eclipse has put in the
closing bracket here now I can implement
the um public forid run method right
here so um and in there I can put the
code that I want to run so let's just
copy this code again contrl C and paste
that in
there and of course once again I mustn't
forget to call um thread. start so t1.
start and if I run that um once again
we've got code running in its own
thread so um that's basically it for this
this
tutorial there are um there is another
way of creating threads using executor
services if you want your threads to be
in a thread pool and um we're going to
look at that in subsequent tutorials but
these are the two basic methods and this
question comes up a lot um in job
interviews and on exams what are the two
basic methods of creating a thread in
Java and that's the answer extend the
thread class or Implement runnable and
pass it to the Constructor of the thread
class in the next tutorial we're going
to start looking at some basic thread
synchronization techniques and we're
going to look at the problems that can
occur if you have two threads um
simultaneously accessing a um shared um
variable or some shared data and we'll
look at what to do about that or at
least we'll start to look at it so join
me again next time and you can find this
code on Cave of programming. comom and
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.