Podcast Lookup

Java Multithreading Explained: Thread Creation, Lifecycle & States | Full Java Course

The Java Developers Hub

  • December 3, 2026
  • 1 h 1 min
Episode page
About this episodeLearn how to create and manage threads in Java in this comprehensive introduction to multithreading. This episode explains how Java…
Learn how to create and manage threads in Java in this comprehensive introduction to multithreading. This episode explains how Java threads work, why they are important, and how they enable applications to perform multiple tasks concurrently.Starting from the fundamentals, we explore different ways to create threads, including extending the Thread class and implementing the Runnable interface. You'll also understand the Java thread lifecycle, the different thread states, and how threads transition between these states during execution.Whether you're a beginner learning Java concurrency or a developer preparing for technical interviews, this tutorial will help you build a solid foundation in multithreaded programming.What You'll Learn
  • What threads are and why multithreading matters.

  • The difference between processes and threads.

  • How to create threads by extending the Thread class.

  • How to create threads by implementing the Runnable interface.

  • How to start thread execution using start().

  • The difference between start() and run().

  • Understanding the Java thread lifecycle.

  • The six official Java thread states:
    • NEW

    • RUNNABLE

    • BLOCKED

    • WAITING

    • TIMED_WAITING

    • TERMINATED



  • How threads transition between different states.

  • Common thread methods and their purposes.

  • Practical applications of Java multithreading.

Who Should Watch?
  • Beginners learning Java programming.

  • Developers interested in multithreading and concurrency.

  • Computer science and software engineering students.

  • Java programmers preparing for technical interviews.

  • Software engineers building concurrent applications

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Multithreading Explained: Thread Creation, Lifecycle & States | Full Java Course

00:00—
  1. Speaker 2

    Yes, so let's start.

  2. Speaker 1

    Welcome back Coder Army to our Java series. And today we are going to go into more depth in our multi-threading. Now we will understand how we can create threads, what methods we can call on threads, and what is the full life cycle of threads. Again, you will have a lot of fun, you will get to learn a lot. So let's quickly go to the screen and start. Yes, so we are talking about the creation of threads. How can we create our threads? In java, everything is an object and we have to create a class for it. So we have to create a class to create threads. There are two ways to create threads in java. One is that java gives us a thread class. You can extend it. Then it will become your own thread, right? Or Java gives us an interface called Runnable Runnable interface Now if it is an interface Then you can implement it Then only your thread functionality will be created How?

  3. Speaker 2

    Let's see how we can create both the uses Let's see that Yes.

  4. Speaker 1

    So here I have taken my demo.java empty file and we have to make our threads And we will perform some operations on it So first of all I told you that we have a way to make a thread First of all, let's see how we extend our thread class So let's say I create a class named myThread and I extend our thread class which is inside java. So I extend the thread class. Now look, the thread class in java has a method which we call run. So I have to override that method. So let's say I wrote public void run. And I have overrided this method. Now what is this run method? Now this run method says that whatever you want to do with this thread, whatever logic you want to perform with this thread, write it in this run method. Now whatever you write in this run method, our thread will perform that logic. Now we will see how this happens. So let's say I want to do a very simple work right now. Let's say I simply write thread is running. Let's say I want this to be printed on the screen. So I made my own thread class. It had a run method. I overrided it. Where does this run method come from? It must come from our thread class. And I printed it.

  5. Speaker 2

    Thread is running.

  6. Speaker 1

    Now I go to my main code. What do I do in my main code? I make an object of my thread. Let's say myThread. Let's say t1 equal to new myThread. I made its object. Now I have to start my thread. So we have a method in Java. t1. start. Start.

  7. Speaker 2

    Okay. Now, as soon as.

  8. Speaker 1

    I call this method, you will see that our thread will start, that is, our output will come on the screen. Now, we just have to print this much, after that our thread will be finished, that is, our thread will be terminated, as we say. And after that, our main method will also be terminated and our code will be finished. Now, let's see how things happen behind the scenes. First of all, let's run it. And see, we got the output, thread is running.

  9. Speaker 2

    No problem.

  10. Speaker 1

    Now let's understand what will happen internally when we write the code. Look, as soon as you write t1. start, you write t1.

  11. Speaker 2

    Start here.

  12. Speaker 1

    So what will happen behind the scene?

  13. Speaker 2

    Behind the scene, as soon as you write the method t1.

  14. Speaker 1

    Start, our JVM told our operating system that I actually want to create a thread. Now who will be able to create threads? Obviously our operating system. So basically I can say that JVM asks OS to create a new thread. So, this happened behind the scene that we have to create a new thread. After that, the operating system must have created a thread. As you know, there can be multiple threads in a process. Until we create our own thread, we always have a main thread by default, which executes our flow. Now, in our process, apart from this main thread, another thread has been created, whose reference variable is T1. Now, we will see what its name is, but its reference variable is T1. And you know what happens when.

  15. Speaker 2

    A thread is created?

  16. Speaker 1

    Thread gets its own stack space and its own program counter So let's say threads get stack as well as program counter space Once thread gets its own space So the thread is ready to be run. After that we can write what will happen internally. Like we have called start. Internally start has called the run method of the thread that we have made. And our thread must have started running. So basically I can write it like this that our thread will execute its run method.

  17. Speaker 2

    Run method.

  18. Speaker 1

    And our thread will start running. Now you know who will execute the thread?

  19. Speaker 2

    Our CPU.

  20. Speaker 1

    That means our thread, which we are calling T1 thread, will get CPU space and it will start running. Right? So this was one way to create our thread using our thread class. Now see, here I have made the same file as demo2.

  21. Speaker 2

    Java.

  22. Speaker 1

    Now what can I do? I can create the thread using runnable interface. Using runnable interface. Now what is this runnable interface? Java provides us with a runnable interface. Anything that can be run, we have to implement that interface. In fact, interestingly, our thread class also implements a runnable interface from inside. So, if I go inside this thread class, it implements runnable. So, what am I doing now? I am not making a thread of my own. I am making a class that implements this interface. So, let's say I write the name of my class as myRunnable because this thread is not runnable. And it implements our runnable interface Now in this runnable interface there is one method again which is run The return type is same as void so public void run And I can perform the work that I want to do with my thread So let's say again I write Same thread is running.

  23. Speaker 2

    Something like this.

  24. Speaker 1

    Now because I have made an object of a runnable interface, not of a thread class, so if I want to run this logic in a new thread, then I will have to make a thread first. Now I will make an object of thread class, t1 equal to new thread. Something like this. And what will I pass in this thread constructor? I will pass an object of our runnable. So first of all, I can make an object of my runnable. So I can write, let's say, my runnable r1 equal to new myRunnable and I can pass this r1 in this thread as a constructor. So basically in our thread class there will be an overloaded constructor which will take a reference variable of r1 named runnable. So if I go into this I will see that I have a constructor which takes a reference variable of runnable named task. So basically I have given it a runnable interface. Now again what I did earlier, I will do the same. I will do T1 as start. Now again behind the scene, it will be the same. As soon as I called start, what did we do? We told our JVM to tell the operating system to create a new thread. A new thread will be created, its stack space and program counters will get it. And thread will execute our run method. This time which run will be executed?

  25. Speaker 2

    This runnable one.

  26. Speaker 1

    So if I run it and show you. then our thread is running and output will come. So if you understood the difference between these two ways of creating a thread, what was the major difference between these two? When we extended our thread class, we were actually creating a thread. Right? Because our myThread was also a type of thread. Because extend means is a relationship.

  27. Speaker 2

    Right?

  28. Speaker 1

    So basically I can write here that when we extended this, we were defining a thread.

  29. Speaker 2

    Defining a thread.

  30. Speaker 1

    But when we implemented our runnable, we were defining a task.

  31. Speaker 2

    So I can write here, defining a task.

  32. Speaker 1

    We were telling what task to perform. Which task to perform in our thread. So we would make an object of our thread class and pass the reference variable runnable as a constructor. So if I show their hierarchy, as I told you, hierarchy is like this. that the runnable interface comes at the top and the runnable interface is already implemented by the thread class in Java.

  33. Speaker 2

    So I can write here that this is our interface and this is our class.

  34. Speaker 1

    And our thread class is already implementing this runable. So when we create our thread in java, extending the thread class, what are we actually doing? We are actually making a sub-thread. Let's say I said myThread. And what does it do? It extends our thread class. Right?

  35. Speaker 2

    So indirectly it is implementing the runnable.

  36. Speaker 1

    Right? So this was the first method I told you about. The second method, I made an interface by calling my runnable. Which will directly implement the runnable interface.

  37. Speaker 2

    Right?

  38. Speaker 1

    Now the difference between my runnable and thread is that when I made my thread here, it was actually a type of thread. So if I have kept the object of my thread as t1, then I can simply call start on t1. I have actually made a thread here. But when I made an object of my runnable, let's say I call it R1, then I have made a task here which can perform a thread. To run this task, I had to make an object of a thread again. Thread is T1 equal to new thread. After that, I used to start T1, right? And when I write thread T1 equal to new thread, I used to pass this R1 in its constructor, right? So the difference between both is that when I extend the thread, I am actually making a thread and when I make my own runnable, let's say my runnable, which will implement my already existing runnable interface, then I am making a task rather than a thread. So let's say I write it here that I have another way to make my own runnable interface, let's say my runnable.

  39. Speaker 2

    Now look, Java has given both the methods. Which is the better.

  40. Speaker 1

    Method according to you? Now look, it's a simple thing. We will always prefer the logic with a runnable interface. There are many reasons for this. The major reason is this. Let's say I have made a class.

  41. Speaker 2

    Let's call this class X.

  42. Speaker 1

    Now I can implement this class with our runnable interface. So implements runnable. And what will I have to do? You know I have to override our run method. So let's say I write void run.

  43. Speaker 2

    I overrided it.

  44. Speaker 1

    And performed some logic here. But apart from this, our whole class can behave like a normal class. Now this x can be our normal class. It can have its own instance variables and methods. It also implements runable. It also has a run method which can run a thread.

  45. Speaker 2

    That's it.

  46. Speaker 1

    The advantage of this is that we can implement multiple interfaces in Java.

  47. Speaker 2

    But we can extend only one class. If you make a class of x.

  48. Speaker 1

    And you extend your thread class, so after that, your x is just a thread and no other class can be extended. Now the whole logic revolves around this thread. So I thought why should we make a thread? Let's make a class that implements the runnable interface. Now it will tell us what task it can perform in its run method.

  49. Speaker 2

    Now, if you want to perform this.

  50. Speaker 1

    Task in any thread, you simply make an object of that thread and pass the reference variable of this runnable interface there. So, basically, if I tell you the benefits, then the first benefit you get is the separation of concern. Now, what does separation of concern say? It says that if we create a class which implements runnable, then we are creating a task. What are we defining here? We are defining a task. Right? Rather than defining a thread. We are telling what we have to do. Right? If we create an object of a thread, then we have to tell how to run it. We don't care about that. We just want to create a task. So, we have separated concern that the thread already exists in Java. We will call that thread. But I have told what I have to do in that thread. Right? Apart from that, our second best advantage is reusability. You have created a class X which implements Runnable. And you have written some logic in it which you want the thread to perform. Now you can give this logic to multiple threads as well. So let's say I have a T1 thread, a T2 thread, a T3 thread. What did I do in all of them? I passed this reference variable called R1. R1 is an object of this one. It has a reference variable. So now these three threads can perform the same logic. So what did I get as a benefit?

  51. Speaker 2

    Reusability.

  52. Speaker 1

    And again, our biggest advantage is multiple inheritance. Because in Java, multiple inheritance is not supported through class. So if I extend thread class to my x class, then I can't extend any other class. But if I have only implemented runnable, then I can extend any other class. And obviously, I can implement multiple interfaces. Okay, I hope you understood this. But look, whether you want to extend the thread class or implement our runnable interface, at the end of the day, behind the scene, you are telling the operating system that I want to make a new thread. So what will it do? Whatever your process is, let's say this is your process. There was a main thread in it till now. Let's say this is your main thread. It will create a new thread for you. Let's say its reference variable is t1. Its name is something else, we will see that now. But I am writing reference variable here. So its reference variable is t1. And obviously you know that it will have its own stack space. It will have its own program counter. And we can run this t1 thread independently. Now, I said that we prefer to implement runnable. Rather than extending the thread because there are many benefits. Let me tell you one more benefit of this. Our runnable interface is a functional interface. This is our functional interface. Now, what is the benefit of functional interface? The benefit of functional interface is that we can use lambda here. So, let's see that in code. So, what I did in demo2. java? I made a class named myRunable and implemented Runable and then overrided Run. And then I actually passed Run here. Since this is a functional interface, I can't pass a reference variable of R1 in constructor and pass a lambda expression. So what can we do? We can simply write, I had to perform logic, I just had to print thread is running. You can simply write, no input, I won't take input, I just want you to print on screen, thread is running.

  53. Speaker 2

    Running, something like this. And our work is done. Simple.

  54. Speaker 1

    Now I don't need to make this class. I will just make an object of my thread. Thread t1 equal to new thread. And I will pass a lambda expression here. Now I don't need this either. I can comment on this. Now when I will start t1. And I will run my program. Then my thread should come after running. Now, interestingly, I told you that the thread that we have created, its name is not t1, it is our reference variable.

  55. Speaker 2

    So, what is its name?

  56. Speaker 1

    How can we call a thread? And why do we need to know the name? See, it's a very simple thing. When you write multi-threading code on production, means you use multiple threads in your code, then your code obviously becomes very complicated. Because a lot of threads are running, running on their own time. And now we will see that the.

  57. Speaker 2

    Thread is not deterministic.

  58. Speaker 1

    Any thread can run first, etc. So it becomes very difficult to debug the multithreading code. In that case, if we know the name of every thread, or a special character, or if we know its ID, then things will become so much easier for us. Right?

  59. Speaker 2

    So let's quickly see how we can know the name and ID of the thread. Okay, so here I have made demo3.

  60. Speaker 1

    Java and here we are going to talk about thread ID.

  61. Speaker 2

    So I'll write thread id and let's say thread name. Thread name. Okay. First of all, forget about all the threads.

  62. Speaker 1

    See, I told you that in Java, whenever we write our main method, what happens from inside?

  63. Speaker 2

    A main thread is always created.

  64. Speaker 1

    Right?

  65. Speaker 2

    Let's first print the name of that main thread on the screen.

  66. Speaker 1

    So let's say I write system.out.println and I have to print the name of my current thread which is actually running.

  67. Speaker 2

    So for that I have a method.

  68. Speaker 1

    So basically our thread glass has a static method currentThread. Now, as soon as I call thread.currentThread, I get a reference of my current thread. And do you know which thread is my current thread?

  69. Speaker 2

    MainThread.

  70. Speaker 1

    So, the thread in which my main function is running, I got a reference of that thread. And do you know what a reference is? If I go inside the currentThread method, it returns a thread-type object. So, actually, there will be a thread-type object in heap, which I got a reference of inside our thread.currentThread. Now, I can call a method inside this, Name. dot get name. Now what did I get from get name? The name of this particular thread, which is my current thread, will be printed on the screen. So basically what I did was I told my thread class to first bring the object of my current thread. So this current thread gave me an object of this thread type. Which thread is this? Which is my current thread, which is basically running. I got its reference variable. I called dot get name on that object. So on my current thread, as I called dot get name, the name of its thread will be printed. Now Can I directly do thread.getName? The answer is no. Because getName is not a static method. It is written here that it cannot make a static reference to a non-static method. So get name is a non-static method that you can call on an object. So what object do I want? I want the object of my current thread. So how will I get my current thread? By calling thread.currentThread. So I printed its name. Now let's print it on the screen. So I got the name main. So this is the name of my current thread. I told you that it also has an id. So I wrote thread.currentThread.getId. So I have called getId method. Now see, it has cut this getId method. Let me tell you why it has cut it. So let's run it and see. So see, my main call is its name and id is 1. Its id is 1 by default. Now see, it has given me some note. So it has told me that your demo3. java file is using.

  71. Speaker 2

    A deprecated API.

  72. Speaker 1

    Deprecated API means, this getId method is deprecated by Java. Java says, stop using it in your code. Because in the coming release, I may remove this method. Whenever you use Java, Java will give you a warning. In fact, whenever you print it, Java will give you a note. That you are trying to use. That you are trying to use a deprecated API. Okay, don't use it. Now what is the reason to remove it? See, it's a simple thing. Java felt that this ID method is unnecessary. It gives you a false sense that it has some meaning. Like what is printed here instead of ID? One.

  73. Speaker 2

    But it has no meaning.

  74. Speaker 1

    In real life, no one calls ID of any thread because it doesn't make any sense. Let's say I made a thread of my own and its id will be 2. Then I made a new thread and its id will be 3. But this 1, 2, 3 is not an ordering. If I start my thread, then it is possible that any thread will run first. What we will see in the future is that the thread shows us a non-deterministic behavior. So here there was no need for a method called id. When he felt this way, he deprecated this method. But let's leave it for now. Now that we are able to use it, I called here. Now I will make a thread of my own and pass a lambda expression in the constructor. The lambda expression will print on the screen. Thread.currentThread.getName. Now when I do thread.currentThread here, what thread will I get? I will get this thread, its reference variable, and I have performed. getName on it. So what print should I have? I should have the RAM print of this thread. So let's do one thing, let's write it properly. I write it something like this, and here I write system.out.println So let's write here name of my thread is and here my thread's name will be printed. So again thread.currentthread.currentthread.getname. getname And just like that, I print my thread's ID on the screen. So, I write here, let's say, ID of my thread is getID. Something like this. So that there will be a little separation. Here our main and its id will be printed. And here it will be printed name of my thread, its name and the thread I made of my own, its id.

  75. Speaker 2

    Now note an interesting thing.

  76. Speaker 1

    I made this thread but let's say I don't call start on this t1. So will my thread ever call? If we run it once, So, here we have just main and 1. Basically, the name of our main thread and its ID has been printed. Our thread has not been called because we haven't started it. We haven't told our operating system to make a new thread for our thread. So, until we don't call this t1. start, sorry, we won't make a thread in our operating system. And our run method from inside, which we have written lambda, will not be called. Now, I have called t1. start here.

  77. Speaker 2

    Now, let's run it once.

  78. Speaker 1

    So you can see, first the main thread is printed, and then the name of my thread is thread 0. The id of my thread is 20. It has randomly put an id here. Now it is not in increasing order, it has randomly generated a unique id of 1, 2, 3, which is of long type and gave it to me by printing.

  79. Speaker 2

    Now you can think, if I.

  80. Speaker 1

    Would make one more thread here, let's say thread T2. Something like this, and here I would print the name of my second thread. 2nd thread is and here also 2nd id is and here I will start t1 dot and start t2 dot so both my threads will start basically two threads will be made in my operating system of my own now if I will print it so you can see main one name of my thread is thread 0 then name of my second thread is thread 1 id of second thread is 21 and id of my thread is 20 Now observe the order of their printing. I have made two threads T1 and T2. In T1, I print two things, its name and its id. In T2, its name and its id. And I started T1 and T2. Now look carefully. First of all, this line is printed. Name of my thread is thread 0. So basically, first of all, the name of T1 is printed. After that, the name of my second thread is thread 1. After that, the name of T2 is printed. After that, the ID of my second thread is printed. And then the ID of my first thread is printed. So you can see that there is no order in this. If I run this program again, then it is possible that these two will run.

  81. Speaker 2

    First and then these two.

  82. Speaker 1

    Or it is possible that the name of this thread is printed first and then this thread. We don't have any order that can be printed in any order. We will see why that happens in our threads. But you can assume that Java doesn't preserve orders. It can print things in any order. For this, if I run it again, So as soon as I run it again, this time the name of the second thread was printed. Meaning it was printed first. After that the name of the first thread was printed. Then the ID of my thread was printed and then the ID of the lower thread was printed. So you can see that there is no order in this. Now again, why do we need the name of the thread? The ID is deprecated so we can ignore it. But the name of the thread gives us benefit for the purpose of logging. When we make a production based application, we want to see which thread is running currently.

  83. Speaker 2

    So we can log it using the name of that thread.

  84. Speaker 1

    In fact, if we look at the methods of the thread class in Java, then we can change the name of the thread class. What is its name? Thread 0, Thread 1. Interestingly, it is not telling you anything. This is its own self-generated name. I want us to keep some of its own names. We also have the setName method. We will see that when we look at all the methods together. But right now, I have made a demo4. java file to tell.

  85. Speaker 2

    You one thing.

  86. Speaker 1

    To explain the difference between our start method and our run method. You must have seen that whenever we create a thread, we call its start method. Right? But in our thread, there is a run method and in our runable there is also a run method.

  87. Speaker 2

    We are doing the overwrite in that.

  88. Speaker 1

    Right? And we write our logic in that. So why don't we call run from ourselves? So look, imagine that I made a thread. Let's say t1 equal to new thread. equal to new thread and let's say I pass logic here that what I want to print I want to print its name so I simply write current thread is and you know our thread class has dot get name sorry dot current thread dot get name get name Something like this. I have created a thread in my code to create an object. But the operating system will not create a new thread until I call start on this T1. Now imagine that I have given a lambda expression in its constructor.

  89. Speaker 2

    What is this?

  90. Speaker 1

    This is an interface of our runnable. If I had written a bigger code, I would have written something like this. I would have made my own runnable class and implemented it. Runnable would have a run method in it. And I would have written logic in it. And then where would I pass all this? In our constructor of thread. Like we saw here. Right?

  91. Speaker 2

    As we saw earlier.

  92. Speaker 1

    So this is the method named run. So if I don't do t1. start here, and call t1. run method here, then what will happen?

  93. Speaker 2

    Okay?

  94. Speaker 1

    Let's see, first of all, let's call our start normally, which we used to do. That we started our thread, we made a new thread to the operating system. From inside, it made a new thread, it got a stack space, it got a program counter. And from inside, it must have called this run method and our logic must have been printed. Now see, because I have made my own thread here, so it should be printed on the screen, current thread is, and whatever will be the name of my thread, let's say thread 0. So I'll print this. And my current thread is thread 0.

  95. Speaker 2

    Very good.

  96. Speaker 1

    Now let's call this t1. run instead of t1.

  97. Speaker 2

    Start.

  98. Speaker 1

    Let's say I'll call the run method by itself. Because it will call the start run method from inside too. So now what will be printed? As soon as I called the run method, the current thread is main. This means that until we don't call start, our operating system will not create a new thread. So if the operating system doesn't create a new thread, then if we call t1. run, basically its run method. What was its run method? It was basically a functional interface. So I said print its logic. So it simply printed its logic that the current thread is So when it will do thread.currentthread, it will get its main thread because our thread t1 is not in our operating system. So when you do thread.currentthread on your main thread, you will get its reference variable. Hence, your name will be printed. So remember, whenever you call t1. run directly from yourself, you are calling run on your main thread because actually there is no thread. Thread will only be created when you call the start method and not the run method. Because start itself tells the operating system to create a new thread, which calls the run method from inside. Interestingly, an interview question also comes up here, which I told you above, that if you call the run method directly, then what will happen? And another interview question is very interesting, that can we start a thread twice? So can we start the same thread twice? Can we do it? The answer is no, we can't do it at all. So, I'll show you how to do it. We've started T1. Let's say I start T1 again. So, as soon as I print it, You will get an illegal thread state exception. This means that your thread is in an illegal state. This means that once you start the thread and it runs and ends, you can't start that thread again. The life cycle of the thread has ended here. Now if we look at the life cycle of the thread, you will understand better. But remember, you can't call a thread multiple times. This is an interview question and its answer is no. Honestly explanation, when we see the thread life cycle, you will understand better. Now see, I have made a file by doing demo 5.java. Because I want to tell you an interesting thing, which is the execution order of the thread. Or I can say the nature of the non-determinism of our thread. Non-determinism. I told you that our thread is non-deterministic. I don't know which thread will run when, which thread will not run when. Now we will see why this happens. But let's say, I first make two threads here.

  99. Speaker 2

    First I made a thread t1 equal to let's say new thread. Equal to let's say new thread.

  100. Speaker 1

    Okay. Now I will tell you the logic of this. Let's say I will make one more thread here. I will have to write the logic of this in it. Basically, I will have to implement runable, so I will have to write its lambda. I am saying that what happens is that the thread that I have made, it prints 1 to 100 but only even numbers. So it prints 2, 4, 6, 8, 10 like this and it also prints 1 to 100 but only its odd numbers. So, all the odd numbers between 1 to 100 are 1, 3, 5, 7, 9 and inclusively, because 100 is even, so 100 should be printed here and 99 should be printed here. So, do you know what I can write? I don't need any input, but what will I give in the output? I will put a loop, let's say for, so let's say I will put a loop for int i equal to, now I have to print even numbers, so let's say, let's run it from 1, i less than 100 less than equal to 100, i plus plus i + + and I will check if the number i is equal to or equal to 0 then I will print it. So I will simply write i or I will write t1 plus i so I will understand that t1 has printed and I will write the same logic here. And I will copy the same logic here. Now I have to print the odd number, so I will write i modulo 2 not equal to 0. So all my odd numbers will be printed. I will write t2 dot and t2 plus i. So I made two threads, one t1 thread and one t2 thread. It prints even number and odd number. Now interestingly I will write t1 dot start and t2 dot start. Normally, if you are a beginner in multithreading, your mind will say that we have started T1 first, so all the even numbers should be printed first, i.e. 2, 4, 6, 8, 10, up to 100, and then we have started T2. So once all the even numbers are printed up to 100, then all the odd numbers should be printed. But because our threading is non-deterministic, Sometimes even numbers can be printed, then some other numbers in between, then some even numbers, then some other numbers in between. You will understand why that happens, we will see now. Before that, let's give a gap here so that we can see better numbers. Okay, so now let's run this. Okay, see here some numbers.

  101. Speaker 2

    Have been printed.

  102. Speaker 1

    First of all, T1 printed 2. Very good, because 2 will be the first even number. Then T1 printed 4, then 6, then 8. It was printed in order. If you look at it carefully, let me lower it a little. So first of all, what was to be printed on T1?

  103. Speaker 2

    Even numbers. So it printed 2, 4, 6, 8 in full order. But then T2 printed 1, 3, 5, 7, 9. 11, 13 also printed.

  104. Speaker 1

    Then T1 printed 10. Then T2 printed 15. Then T2 printed 17, 19, 21, 23, 25. Till here T2 printed. Ok, here is T2 and after that is T1. So you can see that this is printing in any order. Sometimes T1 is printing, sometimes T2 is printing. And in fact, the more you will run it, the more you will get different output. If I will run it again, then this time I will get different output. So this time T2 started with 1, 3, 5, 7, 9. Last time T1 started with 1, 3, 5, 7, 9. So you can see that we are not getting any order in our threads. Now let's understand why this is happening. Now, we understood who executes our threads. Now, obviously, a CPU will execute our threads. Now, you know what a CPU is? It has its own register in which it will store intermediate results of any calculation. So, I will write here that it has its own register. And we call it our own CPU. Here, it is a single core CPU. Now, obviously, we can also have multi-core. So, let's say, as we have made this, we will make another version of it here. Something like this. I will make it a little higher and smaller. So I made two CPUs here. Now we will have multiple threads. So in this case we had two threads. In the case above, we had two threads. One was T1 thread. So in this example, we had two threads. One was T1 and the other was T2. We have two threads ready to run. Because we have called the start method of both of them. That means the operating system said that I want to make two threads. So I made T1 and T2. Now obviously this is in the same process. But CPU doesn't care about this. CPU deals with threads only. So it just sees that I have two threads T1 and T2. Now from inside, CPU has a scheduling function.

  105. Speaker 2

    Scheduling function. Which we also call as scheduling algorithm.

  106. Speaker 1

    And it decides from inside which thread to pick. Now imagine, this CPU picked T1 and started executing its instruction. For the meantime, it picked T2. But if this had happened, it would have been a combined parallelism. Right?

  107. Speaker 2

    Parallelism. So, they are not disturbing each other. There shouldn't be any context switching.

  108. Speaker 1

    And if there is no context switching, then all the numbers of T1 will be printed and the numbers of T2 will be printed parallelly. Parallelly means something like this will be printed. T1 printed 2, then 1 was printed because of T2. Then it printed 4, then 3 was printed because of this. So, odd, even, odd, even or even, odd, even, odd are being printed simultaneously. There is no problem. Another case is that t1 goes here, it executes t1 for some time, then context switching happens, t1 goes back to its place and then t2 starts executing.

  109. Speaker 2

    Then context switching happens and then t1 starts executing. Right? This can also happen.

  110. Speaker 1

    Now even we have multiple cores in our computer, but you also have multiple threads running. Now, for example, in this computer, my program was not running, right? My multiple applications are open in the background, Google Chrome is open, my OBS software is open, right? In which all these screens are being shared. So basically, I already have multiple threads in the operating system which have to schedule the CPU. So the two threads that we made, T1 and T2, I don't know which one will go to which CPU, for how long, after how long will it stop running and then the other thread will go. We saw this in the first video as well. So what I mean to say is that the CPU decides, its scheduling algorithm or its scheduling function decides this. And this is not in my hands. Now this scheduling function is the concept of operating system. We can have multiple scheduling functions. Like round robin, time slicing, priority based etc. There can be many scheduling functions in our operating system. But as a Java developer, it is not in our hands. It decides your operating system and your CPU. Apart from that, it can also depend on system load. How much load is being applied on the system. According to that, it will decide which thread has to be given priority and when to run. And obviously, it will also depend on our hardware. How many cores of CPUs do we have? The more cores there are, the more things will be able to run parallelly. Obviously, it will also depend on the time of thread creation. So I can write here thread creation time. Thread creation time. Let's make it a little smaller. Something like this. So it will depend on this time as well. Now this thread creation time is basically not what we have created in our program. It is when the operating system creates that thread for our CPU.

  111. Speaker 2

    This is the thread creation time. Obviously, if we have.

  112. Speaker 1

    Created a thread in our program, then the operating system will also create it at the same time. But it is possible that T1 gets created a little earlier and T2 takes time to get created because the rest of the programs are creating their threads in the middle.

  113. Speaker 2

    So T1 will run a little longer and then T2 will be able to start.

  114. Speaker 1

    So these are all the things that depend on which thread will execute when. So basically this brings non-determinism. Our code is no longer deterministic. And because of this, there are a lot of problems. If you don't write a good multi-threading code, then you can get a lot of errors because of this. Because it's possible that you expect something, but the output doesn't come. Okay?

  115. Speaker 2

    You may have difficulty in debugging. Because what does debug mean?

  116. Speaker 1

    If your code is not deterministic, If your code is deterministic, then we can debug it. But in non-deterministic code, a specific type of error may occur once and not the next time.

  117. Speaker 2

    So debugging is very difficult.

  118. Speaker 1

    So remember that whenever we write our own multi-threaded code, we should write it in such a way that errors are minimized. We should follow good practice, which we will see in our multi-threading series. So this is a very interesting part of multi-threading. Now if you understand this, we move to the thread lifecycle.

  119. Speaker 2

    Now see, what is a thread lifecycle? We used to create a thread.

  120. Speaker 1

    T1 equal to new thread and then we pass some runnable object here, we pass some lambda here and then we start t1. So according to us, there were only two life cycles in our mind, one we created the thread and one we started it. But actually internally, thread passes through multiple stages, that is called thread life cycle. What are its multiple stages, we will see that now. So let's draw this together. First of all, thread has a new state. Now this is the state when you have created the thread but the thread has not started yet. So when you write this logic in your code, thread t1 equal to new thread.

  121. Speaker 2

    Something like this. And here you also pass a lambda. Lambda.

  122. Speaker 1

    When you write this logic, what does it mean? It means that you are saying in your code that I want to make an object called thread, a reference variable called t1, and a new thread will be created in heap. But you haven't said anything to your operating system yet. You just made a thread in your code. So we call this new phase that you made a thread in your code, but the operating system doesn't know that I have to create a new thread that should be ready to run.

  123. Speaker 2

    So this is the new stage.

  124. Speaker 1

    Now, when you call the start method on your new stage, as you write t1. start, you go to a new stage whose name is runnable.

  125. Speaker 2

    Runnable stage.

  126. Speaker 1

    At this stage, you have told your operating system exactly that I have a thread, make it and start it. That means if you had made a T1 thread, then it is ready to execute. So if you had made a T1 thread and here is your CPU, then it is standing in the queue. So if there are multiple threads here, then T1 is also ready to execute in the CPU.

  127. Speaker 2

    Right? You just called start, right?

  128. Speaker 1

    Now what does start mean? It means that the operating system said that your thread is ready. So T1, T2, T3, let's say you have made these three threads. This is your CPU. These three threads are ready to execute. But what did I tell you? In one bar, in one core, only one thread is executed, right? You imagine that there is no multiple core, I am talking about single core. In one of these cores, only one thread will be executed in one bar. So either it will be T1 or T2 or T3. So the thread that is currently being executed is in a new stage which we call running stage. So let me make it here. We call this running stage. running because now our thread is currently running and you know that for running stage from inside our thread's run method must have been called so right that's why I said we don't call it by itself it is called when actually our thread is running so when this t1 and t2 suppose there are two threads and this is our CPU and our CPU is running t1 let's say here T1 is running So its run method Is being called Right? When contact switching will happen And T2 will start running Then its run method will be called So this is the stage When our run method is being called Of thread Now you imagine Thread is run Performed its work After that thread goes After that thread goes To its last and final stage Which we call termination. Which we call terminated state.

  129. Speaker 2

    Terminated. Basically our thread has terminated.

  130. Speaker 1

    Right.

  131. Speaker 2

    Once your thread has terminated.

  132. Speaker 1

    After that you can't start that thread again.

  133. Speaker 2

    We saw this. Here we get exception.

  134. Speaker 1

    Illegal state exception. Right.

  135. Speaker 2

    So.

  136. Speaker 1

    And how do you reach terminated stage? When your running stage ends. So basically whatever logic you had written in run method. So basically, whatever logic you had written in your run method, that logic is over. Our thread went to the terminated state. So there is no other method to get it to this point. Like if there is a start call, it comes in runnable. If there is a run call, it comes in running. There is no need for any method here. You simply assume that our run is over. So these are the main main phases of our thread. Apart from this, we have more phases, let's understand that. Now the phases that we are going to discuss now, we haven't talked about them yet. We always did this, we created a thread and started it. So we said it came in runnable. When the run method will be called from inside and it will start running, it will be in running state. And when its work will end, it will be in terminated state. Let's assume that you have two threads, T1 and T2 both are in a runnable state, so they are basically ready to execute from our CPU. And whenever the CPU will give time to T1, then T1 will start executing. Now interestingly, think about it, these two threads, T1 and T2 both will have their own run method. Now imagine that our T1 thread and T2 thread both have a common logic written that they have to execute a part of the code. Now that part of code has become a shared resource. Shared resource means, you know, T1 is executing it, T2 is executing it. In that case, I feel that if T1 changes something in it, then T2 will not be able to find out about it. Right? This part of code is generally called critical section in multiple threading. Critical section. And we want to lock the critical section. We will discuss about this further but for now you can take this as an example. Let's say this is our t1 run method. Let's assume this is the t1 run method. Now let's assume what this run method did. This was a student type object that already existed. It was of the name s1. Which can be declared on our code. Let's say it wrote s1. name and changed its name. Let's say it changed its name to aditya. Something like this. So this is the t1 run. Now after this, we will have a run method of t2 as well. And what will it do? It will do the same thing. It will add some other name to the name of s1. Let's say Rohit. Something like this. Now you think, this object named s1 is a reference variable s1. So actually where will s1 be? In our stack. And in our heap, it will have an object. And you know that heap memory is shared memory. So both our threads t1 and t2 will be sharing it. So if both of them in this name variable One will try to put Aditya and the other one will try to put Rohit. So you can understand that my student class has become my critical resource. Because it is shared among all the threads, T1 and T2 are shared. And I don't want both to execute it at the same time. It is possible that I want Aditya to change first and then Rohit to change. But suppose T1 and T2 start running parallel. Both of them will access it. And it is possible that Aditya and Rohit can come at the same time. Same even if contact switching happens and T1 runs first, then it will replace Aditya's name and if T2 runs, then it will replace Rohit's name. Or it can be the other way around, Rohit first and then Aditya. So I won't know what name is happening in the end. So in that case, what I generally do is I say that my student class is a critical resource for me and I don't want all the threads to be able to execute it together. So you can say that my block of code, let's say, where basically s1. name and whatever you want to set, this is a critical resource and I want to protect it.

  137. Speaker 2

    So I do lock on it. Let's say if T1 enters, then it.

  138. Speaker 1

    Will acquire the lock and will not be able to enter inside the T2, T3, T4 or any other thread. Once T1 exits, it will release the lock. Then one of these threads will enter again and it will lock.

  139. Speaker 2

    And so on. These are all locking mechanisms.

  140. Speaker 1

    We will understand this further better when we will understand about locks.

  141. Speaker 2

    Now you may think that even.

  142. Speaker 1

    Now if one of these threads enters, it changes the name and then another thread enters and it can change the name again. So what is the meaning of lock? Even now both threads can enter. We don't have to stop both threads from entering. We have to stop both threads from entering together.

  143. Speaker 2

    And why to stop?

  144. Speaker 1

    You will understand when we will understand about locking. Because there are some changes that we want to be deterministic. Either we execute T1 or T2.

  145. Speaker 2

    Or T2 or T1.

  146. Speaker 1

    But if we don't execute both of them at the same time, it can cause a lot of problems.

  147. Speaker 2

    How can it come?

  148. Speaker 1

    You will understand better when we talk about locks. But you must have understood that it is possible that I don't want both to go together in the critical section and execute it together. In that case, I use the locking mechanism. If T1 is being executed, it will lock the critical section so that no other thread can be executed. And if T2 is being executed, it will lock it with T2 and the rest of the threads will have to wait. So when it happens that a lock is acquired from T1 or T2, then the second thread goes to a new state, which I call the blocking state. So let's say it goes to a blocked state. And when will this happen? When a thread acquires a lock. So here I can write acquire. T1 and T2 were two threads. This was our CPU. T1 acquired a lock. Then context switching happened and T2 started executing. When T2 was executing and it tried to use the same critical section, it will be locked. So T2 will go to the blocked stage. So T2 will never be able to run if it tries to run a critical section which will be locked by some other thread. Now when T1 will start again, it will start again from the inside of the critical section. Now when it finishes the area of the critical section, T1 will release the lock and as soon as T1 releases the lock, T2 will come back from our blocked state to our runnable state. That means it will be ready to run again. So I can write lock released. Interestingly, it's not just about the lock. You can assume that one of the two threads, T1 and T2, is trying to take input and output from the user.

  149. Speaker 2

    Let's say we have only one CPU. And it was executing T1.

  150. Speaker 1

    And T1 said that it's trying to take input from the user. But the user won't be able to enter the name. We won't be able to proceed in this case. So T1 will go directly to the blocked state so that another thread can be run until the user gives input and when the input comes, it will go back to its runnable state from the blocked state. So let's write IOTASK here as well. So both the cases can be here. If you want to do IOTASK, you want to acquire the lock, then it goes to the block and if it is done, then the lock is released and it goes back to the runnable state. Now here I have done a wrong thing, I have put it in a runnable block state, which is wrong. You think T1 and T2 were two threads. I said this was our CPU. I said T1 is running. And it acquired a lock. And then contact switching happened. Now T2 started running. Now when T2 tries to acquire the lock, then only it will be blocked. And when will it be able to try when it was running? So I should not join it from here actually. Logically, this should be joined to our running state. But in Java, these two stages, runnable and running, are considered to be the same stage. So basically, my diagram will be something like this. Like this and like this. I will enter from here and exit from here. Because for me, these two are the same stage. Now why do you think so?

  151. Speaker 2

    Think logically.

  152. Speaker 1

    We can just start a thread, right? We can't run it ourselves. And it's not in our hands when which thread is running. It's the scheduling algorithms and operating system of CPU that decides. For us, these two are the same stage. So, whether our thread is ready to run or actually running. So, whether our thread is in the waiting area, let's say CPU. T1 and T2. And if T1 is running, then T2 is in the waiting area. Or if T2 is running, then T1 is in the waiting area.

  153. Speaker 2

    So whatever it is.

  154. Speaker 1

    But these two threads, T1 and T2 are ready to run. For us, these two are the same stage. And Java knows them as runnable. We just make running conceptually from inside. But running is not a stage. It is a stage called runnable from inside. Which we represent like this.

  155. Speaker 2

    Apart from this, there are two more stages.

  156. Speaker 1

    One stage is our waiting stage.

  157. Speaker 2

    Waiting. And when will any thread come in this stage?

  158. Speaker 1

    Any thread will come in this phase only if you have called the wait method on that thread. Now we will see this method properly. These two threads are very useful to talk to each other. And when will it go back from this stage? It will go back from this stage when you call the notify method on the thread on which you have waited. So as soon as you call notify, it will wake up and come back to its runnable stage.

  159. Speaker 2

    Now we will discuss.

  160. Speaker 1

    About this waiting stage in depth later. Let's go to our new stage. which is time-advaiting. very interesting stage. It says that we can make a thread wait for some time. We can make it wait time-specific. What can we do? We can call some method on it. Let's say sleep is a method. Here join is also a method. In fact, join can also come here. We can also call join here to put it in wait stage. We will discuss that further when we come to all the methods. And here, in fact, wait can also come with an extra parameter, which will be time. So if we give time here, here too, and if we give time in sleep, then our thread, which we have sleep, will go to the waiting stage for some time. And when that time will end, when that time will end, it will come back to our runnable. So these are all the phases of our threads. Generally we call it one phase, two, three, four, five and six phases. We consider our two phases in one phase, which is the runnable phase. Interestingly, you must have understood this phase that how to go from new to runnable and how to go from runnable to terminated. These three cases are special, we will discuss them well as we move forward. What does blocked stage mean, what does waiting mean and what does time mid waiting mean? Now look, we have a very interesting method.

  161. Speaker 2

    Stop.

  162. Speaker 1

    Stop. Although this method is now deprecated. Java doesn't want you to use it. And why it doesn't want, we'll see that later. But you imagine, if we use this method, then our thread.

  163. Speaker 2

    Whether it's in any phase, okay? So whether it's.

  164. Speaker 1

    Be it in new phase, runnable phase, blocked, waiting or timed waiting if we call stop on it then it will go straight to terminated state so basically I can draw it like this we will go from new to terminated we will go from runnable to terminated and from our blocked waiting and timed waiting we will go to terminated if we call stop from anywhere from here to here if we call stop from anywhere then we will go straight to the terminated stage. This is not a good method, we should not use it. It brings a big problem which we will see in the future.

  165. Speaker 2

    But this is the life cycle of your thread.

  166. Speaker 1

    It may seem complicated to see the diagram, but if you construct it step by step, then you will find it very simple. In fact, we will study our waiting stage, timed waiting, blocked stage in depth But you must have understood new phase, runnable and terminator Yes, so I have made a file called demo6. java In which we will see all the phases of our threads, thread life cycle We will understand it properly So see, first of all I said thread creation stage Now see, interestingly there is an enum inside our thread glass Which we call state So thread.state If we go inside this enum Then you will get all the states of the thread New Runnable Blocked Waiting Timed waiting And terminated Which I talked about I told you that there won't be any running stage Because it will come inside the runnable This is an enum So we can use this enum To find out Which thread is at which stage So first of all we see The new stage of our thread So thread new stage Let's comment this Ok So let's say I wrote thread t1 equal to new thread and here I wrote a simple logic like we were always writing I simply print here name of current thread is and here thread dot current thread dot get name call So my thread name gets printed Now I have only created the thread and not started it. So this logic will not be printed until I start the thread. But if I print the T1 thread and call its getState, it will bring a state of its current thread. So as soon as I call its getState, let's see which state will be printed. So you can see that the state is printed as new. That means my current thread is in new state. Now see, here I write t1. start What did I do here? Here I brought it to the runable stage. Runable stage. You know what will happen from inside. I would have told my operating system to make a new thread and make it ready to run. So here it is at the runable stage. Now if I quickly do t1. getStage again. getState getState Okay, so my runnable state should be printed here. Now see, first of all, my new state should be printed. After that, I am doing t1. start and then trying to print my state. Now there are two scenarios here. One scenario is that as soon as I started t1. start, I said that you are ready to run. But it is possible that you have not run immediately, you have not created the thread, which is a high chance. Because immediately I wrote in the next line, system.out.println t1. getState, whatever its status is, print it. So first its state will be printed which means our runnable state will be printed after new. After that its logic will be printed which is name of current thread is let's say thread 0.

  167. Speaker 2

    And then it will end and our program will end.

  168. Speaker 1

    But the second scenario which is very unlikely is that as soon as I started t1. Our operating system made a thread and it started running quickly. And as soon as it starts running, the current thread name is 0. And as soon as it prints, the thread is gone. And when we do system.out.println, our runable is terminated.

  169. Speaker 2

    Terminated.

  170. Speaker 1

    But why am I saying that the chances of it being terminated are very low? Because it takes time. To start T1. 1, the operating system creates a thread and keeps it ready. Till then, the chances of our next statement being run are almost 100%. But again, I said that threads are non-deterministic. So there can be a rare case that your terminator gets printed here. Let's run it now. So yes, our runnable is printed here. First new stage, then runnable stage, and then its logic, name of current thread is thread 0. Now, interestingly, I am saying that I also want to see terminated state. So to see that, I can write get state again here. So I wrote it once and expected that by now my t1. start would have gone, this would have come and it would have gone to terminated. So I should have a terminated print role here. Right?

  171. Speaker 2

    But will it happen? Let's see. Not at all.

  172. Speaker 1

    I have a runnable print twice. That means it was not finished yet. What if I write it again? Right?

  173. Speaker 2

    I am trying to finish it. But I wrote it 3-4 times.

  174. Speaker 1

    Will I have a runnable print every time? See this, every time runnable, runnable, runnable, runnable. And then this thread runs. Means, my print statement executes quickly before my this thread executes. Now, it's possible that I write 100-200 times, so in the end, some terminator prints. But generally, all of these will print our runnable. Because this print statement executes first after it executes. Because as I told you, it takes a lot of time. That our operating system will make a thread, it will get that CPU time. So the best way is that I will sleep my main thread for some time. So wait for some time. So the best way is that I write. thread.sleep let's say 2000 means this is milliseconds so I said sleep for 2 seconds and this is my constant thread current thread see here I am getting an error and that error is unhandled exception interrupted exception so whenever I write thread.sleep this exception throws me so I write it in try catch block so try catch let's say exception I handled it as E and I left it empty for now. So I made my main thread sleep. There is a static method called sleep in thread, you will see it later. I said 2000 milliseconds. I said 2000 milliseconds means sleep for 2 seconds. Now I know this thread must have run in this much time. After that if I print my get state again. Basically something like this. So now I'm most probably sure that my terminated state should be printed. Right? Let's check it out. Just remember that our main code will hold for 2 seconds. When will it hold? When we have created the T1 thread, we have started T1, once we have printed our new state, once we have printed our runnable state, then we will tell our main to stop, let T1 run, then T1 will print its logic, and then we will print our terminated state.

  175. Speaker 2

    So let's run it once.

  176. Speaker 1

    So now look, Here we have a 2 second hold. First we have a new print, then runnable. Then our main thread stopped for 2000 milliseconds. And till then our T1 thread started working.

  177. Speaker 2

    So it printed this.

  178. Speaker 1

    And then it finished within 2 seconds. And then our stage printed terminated.

  179. Speaker 2

    Right?

  180. Speaker 1

    So we have printed our three stages here. Interestingly, when our main thread is sleeping, I want someone to tell me what is the stage of this main thread.

  181. Speaker 2

    Its stage should be timeid waiting. But how can I print that?

  182. Speaker 1

    If I try to print it here, let's say I write thread.currentthread.getstate. So let's say I wrote getstate here. Will my timeid waiting be printed here? The answer is no. Because it has not been sleeped yet, it will be sleeped from the next line. And as soon as it will be sleeped, I will not be able to print anything in the main thread. So I can't write it here. There is a very interesting way to print the state of the main thread after the name of current thread. And how can I do that?

  183. Speaker 2

    Look, if I.

  184. Speaker 1

    Write simple, then here I will first bring the reference of my main thread.

  185. Speaker 2

    So let me take that first. This is very interesting. This is very interesting, you will enjoy it a lot. Let's say I wrote.

  186. Speaker 1

    Thread let's say main thread equal to thread dot current thread so I took reference of my main thread in main thread and here I write main thread dot get state so it will print our main thread's state when my main thread is getting sleep right because as I started it what I did is I printed its state and made main thread sleep so till my main thread is getting sleep my t1 is getting run as you know in the background So when it was getting run I said I will print its state of main thread So let's see what is printed So here our time hit waiting stage is also printed So here we printed our new and here we printed our runnable.

  187. Speaker 2

    We know all this.

  188. Speaker 1

    After that, as soon as we sleep our main thread and our current thread starts printing, it first printed the name of current thread is thread 0 and the main thread's state is timeid waiting. And finally, our normal thread t1' s terminated state was printed from here. So, you can play like this to print different states. We haven't seen the blocked and waiting state here because again, I will need locks for that. We will understand that later. And for waiting, you can also call wait on the other side. But we will understand that in-depth later. But for now, I hope you have understood how our different states interact. You just have to remember one interesting thing here. That our blocked and waiting state, whenever our thread is in this stage, So all those locks which we call Monitor Locks, they release them. Monitor Locks means if it has acquired a lock, then it will release it for other threads. But our timeid waiting stage does not release Monitor Locks. Again, you will understand this better when we will understand locking mechanism. But I hope for now you have understood the thread's life cycle well. You must have understood how we can create threads in different ways. In the next lecture, we will see the methods of threads, what are demon threads, we will see that. That's all for this lecture. See you in the next video. Till then, thank you so much.