0:04 hello this is John from Cave of
0:07 programming. comom and this is the first
0:09 in a series of tutorials on
0:12 multi-threading in Java so in this
0:14 tutorial we're going to look at starting
0:17 threads in Java and next time we're
0:19 going to move on to looking at basic thread
0:20 thread
0:23 synchronization so I'm here in eclipse
0:27 and I'm going to create a new project
0:29 let's create a new Java project and I'll
0:36 one and I'll give that a um a main
0:39 class uh which I'll put in a package
0:42 called demo one so let's call this just
0:47 app and I'll put it in the demo one
0:50 package now uh there are two basic ways
0:54 of starting a thread in Java and a
0:55 thread is just kind of like a separate
0:58 operating system process which can run
1:01 concurrently with other
1:04 threads and uh the first way of starting
1:08 a thread in Java is to extend the thread
1:12 class so let's say here class um I'll
1:17 call it Runner um extends
1:20 thread now the thread class has a method
1:24 called um run and I want to override
1:26 that method and put some code in there
1:29 so I'm going to right click here go to
1:32 source override Implement methods and of
1:34 course I could also do this by hand and
1:37 the signature is just public void
1:40 run so I'll override that and get rid of
1:43 this unnecessary stuff here now in run
1:45 I'm going to put some code that I want
1:48 to run in its own separate
1:51 thread and um I'm just going to put some
1:53 code in here to simulate doing some
1:56 useful work so let's have a loop I'll
2:02 say for in I equal not I less than
2:10 i++ and I'll just output here some text
2:13 with a system out I'll say sis CIS out
2:17 control space to to complete and I'll say
2:18 say
2:21 hello and let's just add on to the the
2:24 loop index now to make this a more
2:26 realistic simulation I'll slow this Loop
2:30 down a bit and I'll put in here
2:33 I'll put a I'll use a static method of
2:36 the thread class called
2:39 sleep and um sleep pauses your program
2:42 for the specified number of milliseconds
2:44 so let's just pause this for 100
2:48 milliseconds which is 1/10th of a second
2:50 and um annoyingly thread. sleep throws
2:53 an interrupted exception or can do so I
2:55 need to just handle that exception here
3:02 work so to to actually use this class
3:04 and to actually run this code in its own
3:07 thread I need to declare an instance of
3:09 my class down
3:13 here I'll just call this Runner one and
3:16 I'll set that equal to a new
3:19 Runner and to run this code I need to
3:22 type Runner one um do start so I'm
3:26 calling the start menu of the thread
3:29 class um it's important not to call run
3:31 because if I call run it certainly will
3:34 run my code but it's going to run it in
3:37 the main thread of the application if I
3:39 call start that tells the thread class
3:42 to go and look for the run method and to
3:45 run that in its own special thread so
3:47 that's why I'm calling start
3:50 here and if I run that now um I'll see
3:53 some code and it's just running and it's
3:55 actually running in its own thread and
4:00 to prove that um let's just copy this
4:04 and I'll have another Runner Runner
4:07 two and I'll run them both at the same
4:10 time so if I run this now and you can
4:13 see we've got here um interleaved output
4:16 so let's scroll to the top I've got 0 0
4:20 1 1 22 and so on so both Loops are
4:22 running concurrently they're running at
4:23 the same time and that's kind of the
4:26 whole point of threads that you can run code
4:28 code simultaneously
4:30 simultaneously
4:32 so that's the first basic method of
4:35 starting a thread in Java the second
4:38 method is to implement runable and pass
4:40 it to the Constructor of the thread
4:43 class so I'll create a new main program
4:45 here and its own package to demonstrate
4:47 this I'll call it app again and I'll put
4:51 it put it in a package called demo [Music]
4:53 [Music]
4:57 2 and um I'm going to create a new class
5:01 up here class class I'll call it Runner
5:03 again and I'm going to say that Runner implements
5:05 implements
5:09 runnable and runnable is an interface
5:11 which just has one method in it public
5:15 void run so let's um let's just click
5:17 this eror here and go to add
5:20 unimplemented methods and now I've got
5:23 my public forid run method there and
5:26 just as before so this is the last slot
5:29 of code um I'm going to put my code that
5:31 want to run in the public void run
5:34 method so I'll just copy
5:38 that and I'll paste in to the run method
5:40 of my class that implements the runnable
5:43 interface and now to run that I'm going
5:45 to declare an instance of the thread
5:48 class I'll call it T1 and I'll set that
5:50 equal to a new
5:54 thread and I'll pass an instance of my
5:57 Runner class here to the Constructor and
5:59 the quickest way to do that is just to
6:01 say say new
6:04 Runner and to show that I can get code
6:06 uh running at the same time I'm going to
6:07 have two of
6:10 those let's just copy
6:13 that and I'll have
6:16 T2 and now to actually make the code run
6:19 I need to call t1. start and of course t2.
6:21 t2.
6:24 start and if I run that now we're going
6:27 to see the same thing again down here
6:31 let's just maximize the set and run it
6:33 um so I've got inter leave output once
6:35 again and both um threads of Co are
6:38 running at the same
6:41 time now sometimes um you just want to
6:45 run like one method in its own thread
6:47 and it seems like a lot of hassle to
6:50 create a whole separate class and uh
6:52 there is just like a quick way that we
6:56 can do um either of these um things
6:58 really and I'll just demonstrate that
7:01 here so this isn't a new way of um
7:04 running a thread it's just um a
7:06 technique using an anonymous class that
7:08 comes in handy sometimes and let's put
7:11 this in a package called demo
7:16 3 and um get rid of this stuff so to run
7:18 just a little bit of code you can do
7:20 something like this I'll say
7:30 thread and um here
7:32 in inside the Constructor brackets of
7:35 the thread class I can say new runnable
7:38 round brackets just as I I was trying to
7:40 create an instance of a class called
7:43 runnable which I'm not um but if I open
7:45 a bracket here an eclipse has put in the
7:49 closing bracket here now I can implement
7:52 the um public forid run method right
7:57 here so um and in there I can put the
7:58 code that I want to run so let's just
8:03 copy this code again contrl C and paste
8:04 that in
8:07 there and of course once again I mustn't
8:11 forget to call um thread. start so t1.
8:14 start and if I run that um once again
8:18 we've got code running in its own
8:21 thread so um that's basically it for this
8:22 this
8:25 tutorial there are um there is another
8:28 way of creating threads using executor
8:31 services if you want your threads to be
8:34 in a thread pool and um we're going to
8:36 look at that in subsequent tutorials but
8:39 these are the two basic methods and this
8:42 question comes up a lot um in job
8:45 interviews and on exams what are the two
8:49 basic methods of creating a thread in
8:51 Java and that's the answer extend the
8:54 thread class or Implement runnable and
8:57 pass it to the Constructor of the thread
8:59 class in the next tutorial we're going
9:02 to start looking at some basic thread
9:04 synchronization techniques and we're
9:06 going to look at the problems that can
9:10 occur if you have two threads um
9:14 simultaneously accessing a um shared um
9:16 variable or some shared data and we'll
9:18 look at what to do about that or at
9:20 least we'll start to look at it so join
9:22 me again next time and you can find this
9:26 code on Cave of programming. comom and