Podcast Lookup

Java Inter-Thread Communication Explained | wait(), notify() & notifyAll() Deep Dive

The Java Developers Hub

  • December 3, 2026
  • 1 h 3 min
  • Episode 19
Episode page
About this episodeIn multithreaded Java applications, threads often need to communicate and coordinate their execution to work together effectively. Java…
In multithreaded Java applications, threads often need to communicate and coordinate their execution to work together effectively. Java provides built-in mechanisms that allow threads to wait for specific conditions and notify one another when those conditions are met.In this episode, we take a deep dive into inter-thread communication using the wait(), notify(), and notifyAll() methods.You'll learn how these methods work, how threads coordinate access to shared resources, and how to implement synchronisation patterns such as the Producer-Consumer problem.Whether you're learning advanced Java concurrency or preparing to build reliable multithreaded applications, understanding inter-thread communication is essential.

Transcript

From the publisher

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

Java Inter-Thread Communication Explained | wait(), notify() & notifyAll() Deep Dive

00:00—
  1. Speaker 1

    Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to talk about inter-thread communication. And what is inter-thread communication actually? You will know all that in this video. You will have a lot of fun. So let's quickly go to the screen and start. Yes.

  2. Speaker 2

    So what is inter-thread communication?

  3. Speaker 1

    Obviously, we make a lot of threads Let's say T1, T2, T3 and so on Now we know that sometimes one thread interferes with the communication of another thread Which we call thread interference Now it is possible that they try to fetch the shared resource And because of that they change something So T2 may have a problem Because of which we introduce a lock mechanism Like we saw synchronize It locks a critical section area so that only one thread can be entered at a time. But this synchronize can't control everything. What are the things that it can't control? For that we have to look at inter-thread communication. And here we have three important methods. One is wait, one is notify and one is notify all. And these three methods are found in our object class. Right? When we were talking about object class, you had read about these three methods. Now, what are these three methods? What are they used for? And again, how do they do inter-thread communication? We will know all that. Now, for this, we will start with an example. Now, this example that I am telling you is a very popular example in multi-threading. Which we call producer-consumer problem. Producer-consumer problem. Okay? If you have never heard about it, then let me describe what this problem is and how we solve this problem. So, imagine that you have a producer and the job of the producer is to produce some content or data. And let's say he produces some data and puts it in this empty space or container. It may be a variable like this box. So, the producer produces some value and puts it in this box. And at the same time, we have a consumer whose work is to consume the value. So, what will the consumer do? He will consume the value produced by the producer. Let's say this producer put a value of 5 in this box. So, the producer.

  4. Speaker 2

    Produced this value.

  5. Speaker 1

    Now, the consumer's work is to consume this value. It means to consume 5. Once the consumer consumes 5, the box will be empty again. After that, the producer will produce the value again. Let's say he produced 10. Consumers will consume it and our box will be empty again. Now, why do we call it a problem? We call it a problem in multi-threading because whenever we write a multi-threaded code, it becomes non-deterministic. Now, there can be many scenarios here. Now, imagine that our consumer runs first. So, this is also running on a thread, let's say T1 thread and this is also running on a thread, let's say T2 thread. So, T1 thread tries to consume something from here and the producer has not produced any value yet. So, how can we write that the consumer tries to consume Let's say null value, something like this. And what can be the second scenario? The second scenario is that the producer has already produced a value. Let's say he has already produced 5 here. But the consumer hasn't consumed it yet. So, the thread T1 might not have got the chance to run. Now, the producer runs again. So, what if the producer produces a value again and replaces this 5 with a new value 10? Then our old data will get lost. So, I can say that the producer produces a value Without consumer, consume the previous one. So these two scenarios can be understood which can be a problem in multi-threading. And what is the box here? Box is nothing, it is a container in which the producer produces value. This is a very classic problem, producer-consumer problem of our multi-threading. And it shows how two threads need to communicate a lot. Because if the consumer and producer are not in sync, then either the consumer will read the empty value in this box, or it is possible that the producer will add multiple values, replace one value with another value, and the consumer has not consumed the last value yet. So let's see the code of this problem and then see how we can solve it. So here I have taken demo.java as an empty project And let's say I make a box class here Which I had said is a container box And it has only two things Let's say one is our integer value So let's write it as capital integer So that I can store it as null So integer value Something like this And it has a boolean value Let's say a boolean flag Which is initially false Now see, I have made a box in which there is a value of integer type and a flag. Here the spelling of flag is wrong. So flag tells whether there is any value in this value. Let's call it item so that there is no confusion. So flag tells whether there is any value stored in this item. Currently flag is false meaning this item is null because I haven't stored any value in it. Now here I make two methods. I make one method producer. We will make it short.

  6. Speaker 2

    Producer.

  7. Speaker 1

    It will take a value of int type and will store that value in this item. So we will do item is equal to value and will make this flag true. Now we have a value in it. It made the flag true. And let's say we have printed which value we have produced. So let's say we write producer produces.

  8. Speaker 2

    Item.

  9. Speaker 1

    So basically we can print the value as well as the item. I have printed the item here. So basically what we did is we put a value in this item, we made its flag true, and we printed it. It's a very simple method. Similarly, we make another method which we call let's say consumer. And the consumer's job is to consume this value. Now what does consume mean? Basically, it's a simple thing. First of all, it will print that I have consumed. So consumer consumes consumes Consumes let's say item whatever was stored in it and after consuming it will null the item and will false this flag again something like this Okay, so now we understand that there is a producer who produces value and a consumer who consumes value. Now in real life, producer-consumer is a very classic problem because when you look at it, in computer science, many things work like a producer-consumer. Now we will look at its examples, like a printer is an example of it. Now we will look at many of them. First, let's look at this problem. Now imagine that I make two threads in my main code. So let's say I make a thread. First of all, let's make an object of this box. So let's say box B1 equal to new or let's call it box. Small box equal to new box. Something like this. And we do. Equal to new box. Something like this. And let's say I make two threads. I make one T1 equal to new thread. Equal to new thread. And what do I do in this? Let's say I call the consumer of our box in this. Let's call producer as box.producer. And I will give a fixed value of 10. Now I have to make a consumer thread. So I will make a consumer thread. I have made a new thread named t2 and it calls box.consumer. And here we will not give any value So box.consumer So I made a t1 thread and a t2 thread Apart from that you know what I do I start t1 And start t2 too So now we will produce each value Now if the producer runs first So someone will put the value in it Let's say he put 10 here We are putting 10 here So we put 10 here It will make the flag true And it will print I have produced value 10 And after that if the consumer runs Then the consumer will print That I have consumed 10 items will null the item and the flag will be false again. But if it runs upside down, we know that our multithreaded code is non-deterministic, so if the consumer runs first, then it will print consumer consumes null, will null the item again, will false the flag again, which will already be false. And after that, if the producer produces, then there is no point, the producer will produce, but the consumer has run once. So if we run it just once, then there is a 50% chance that any value can be run. So first the consumer runs and consumes null because there was no value before. After that, the producer produces 10 value. So we understood that there was a 50% chance here. If the producer runs first, then he puts the value of 10. If the consumer runs first, then he tries to consume null first. Now see, what if I increase the number of iterations here. So I will put a loop here, let's say for int i equal to 1 to less than 20, i less than 20, i plus plus. So 20 times producer will work and every time it will print our box.producer. Producer will run and every time it will try to add i value.

  10. Speaker 2

    So first time 1, then 2, then 3 and so on. And what can I do in the same way?

  11. Speaker 1

    I can also run my consumer 20 times because if I am producing 20 values, then I will consume.

  12. Speaker 2

    20 values as well. Let's remove it like this.

  13. Speaker 1

    Okay. And what I do is to mimic that it takes time for the producer to produce some value and it takes time for the consumer to consume, I put here, let's say, sleep. So you know I can write thread.sleep.sleep and not more than 100 milliseconds. For 100 ms, the producer holds before producing. So I'll catch exception here.

  14. Speaker 2

    Catch exception. Okay.

  15. Speaker 1

    And I'll set the same time before consuming. in our consumer. Okay.

  16. Speaker 2

    Okay.

  17. Speaker 1

    And these two times are kept a little different. Let's say consumer only takes 70 milliseconds. Okay. So consumer is working fast. Producer is working a little slow. Now you know because multi-threading code does not have determinism. Consumer will work fast and producer will work slow.

  18. Speaker 2

    So many times it is possible that consumer will read our null value.

  19. Speaker 1

    Okay. So let's run this once and see what happens. I have run it. So see, our value is getting printed slowly. First of all, the consumer did null print. Obviously, there was nothing to consume. Then the producer came, he produced 1. The consumer consumed 1. Then the producer came, he produced 2. The consumer consumed 2. Then the consumer came again. See, he did null read because there was nothing. Producer did 3, consumer did 3, 4, 4. Okay. Then consumer will go again and will read null. Okay. 5, 5, 6, 6 and so on. So many times it will happen that consumer is reading null. Many times it will happen that producer produces an extra value. So our other value gets overrided. Okay. So I hope you understood at basic level that what is producer-consumer problem. So if you understand this, then many people will think that what if we use synchronize in both?

  20. Speaker 2

    Synchronize? We know what synchronize does.

  21. Speaker 1

    It locks our entire block and in one block, a producer or a consumer can enter because our lock will be on our object. But here, synchronize will not be able to solve the problem. Okay, why won't it be able to solve it? Think carefully. If we had this producer code And let's say below this is our consumer code You can imagine whatever our code was And I had said that if the producer enters first Then he will produce some value let's say 5 And if the consumer enters then he will print 5 But if the consumer enters first then he will print null Because he won't be able to consume anything Or if the producer enters twice continuously Then he will replace this 5 and print a new value Or produce it So these were all the normal problems Now you are asking us to synchronize it. But what will happen by synchronizing it? By synchronizing it, we make sure that let's say we have two threads. One thread is T1 and the other is T2. And T1 works to produce. So what will it be doing from inside? It will be calling the produce of this box. Imagine this is our box. Box means our class. This is the box class. There are two methods written in it. One is producer and the other is consumer. So T1 will basically produce calls and T2 will consume calls. Okay, now let's assume I have protected them. I have basically put synchronize here. Okay, so this is also synchronized. This is also synchronized. Okay, so synchronize will just make sure that if the producer enters, then the consumer will not be able to enter. Or if the consumer enters first, then the producer will not be able to enter in it. But it will not be able to make sure that the consumer does not enter first from the producer. It will not be able to make sure the interactions between them.

  22. Speaker 2

    This is just odd.

  23. Speaker 1

    It can only make sure that only one of the two will enter at a time. Right? So, synchronize can't solve our problem because our problem is different. Our problem doesn't come from the locking mechanism. Our problem comes from the communication of these two threads.

  24. Speaker 2

    Okay?

  25. Speaker 1

    We want these two threads to coordinate each other. If the producer doesn't have any data available, then the consumer shouldn't consume it. And if the data is available already, then the producer shouldn't produce new data. Okay? So, I hope you understood the problem. So, what is the solution to this problem? There is a very naive solution to this in our mind. What is that? Let's put an infinite loop. Basically, the value that we are going to produce in our producer has an int value. And what does it do? It writes some code here, which is our item. Put this value in it. And it trues its flag And prints system.out.println on the screen And it writes I produced this value It works like this Imagine I write a condition in this part of my code And I write a condition like this while Until the item which is declared in the box is not equal to null, until there is a value in it, you should keep moving in this loop. So our producer will keep moving in this loop until null is stored in that item. And I will put the same condition in the consumer. We have a consumer whose job is to consume the value. So what he does is, he stores null inside the item. Something like this. Before that, he would have printed on the screen that what did I do? I consumed the item value. And I will write a logic here as well. I will write here while item is equal equal null. Till the time the item is null, you also keep on rotating in the infinite loop. So if the consumer enters first, then he will come in this line, item equal equal null. Now the item is null, the answer is yes. So he will keep on rotating in this loop.

  26. Speaker 2

    Till when?

  27. Speaker 1

    Till the time the thread of any producer does not enter in the producer. Now if the item is null, then the condition of item not equal to null will fail. That means the producer will skip this and store a value in the item. Now as soon as he will store the value, If there is any value in our item, then this condition will fail.

  28. Speaker 2

    This means that the consumer can go to consume.

  29. Speaker 1

    So what I did is that I put a coordination between these two. Now you will say that he will read it with his cash. No problem, we will use the volatile keyword that you have read. So that he does not read with his cash. We have seen this problem in our old videos. So our problem will be solved. But you understand what is the problem in this. Now, in this approach, a big problem is that our thread, whether it's a producer's thread or a consumer's thread, if it's rotating in an infinite loop, like here or here, then it's consuming the time of the CPU and is not doing anything productive. So, let's say if the consumer enters first and the value is null, then the consumer will keep rotating here for a long time until our value is produced. And let's say for some reason, the producer takes 2 seconds to work here. We just mimic it, so we put thread.sleep. But understand real-life scenarios. The producer could be an external server that is producing some value. Now, it's possible that it took 2 seconds to produce any value. Till then, the consumer's thread will keep running in this infinite loop for 2 seconds. And it will consume the time of CPU. Because CPU doesn't know that it's not working. CPU only knows that it has to execute some instruction. So what will CPU do? Our CPU will try to give the time to the T2 thread to the T2 thread. It will also switch the contact in between, but as soon as it comes back, it will run again in an infinite loop. So it is consuming the time of the CPU.

  30. Speaker 2

    And we specifically call this kind of problem busy waiting.

  31. Speaker 1

    So it is.

  32. Speaker 2

    Busy only in waiting. It is busy in waiting. It is not doing anything productive.

  33. Speaker 1

    Now our thread is checking again and again Like the consumer is asking again and again the question What item is available?

  34. Speaker 2

    What item is available?

  35. Speaker 1

    As soon as the item is available, it will consume it And the same producer is asking again and again What item is empty? What item is empty? As soon as the item is empty, our producer will produce it Right? So let's try to solve this once So this again came in our demo2. java, I copied the same old code here We have a box class with item and flag And here is producer and consumer, okay very good And we had two threads running here, something like this So now here we implement our naive approach What was the naive approach? I was saying that producer will not produce a straight value First it will run in an infinite loop, so they will do while Now we can test it with flag. So flag equal equal false. And equal equal true. If flag is true. Means there is already value here. So I can write do nothing. Don't do anything. Okay.

  36. Speaker 2

    Sit like this.

  37. Speaker 1

    Keep roaming in your infinite loop. Until flag's value becomes false. Same like this I can put an infinite loop in my consumer.

  38. Speaker 2

    Something here.

  39. Speaker 1

    Okay.

  40. Speaker 2

    Something like this.

  41. Speaker 1

    And here I can write flag = false, then do nothing. Now look, here I will have to put volatile keyword. You already know this. Why? Because suppose if it stores the value from the item, then it is possible that the consumer will never know about it. So basically, here we have to put So let's assume that flag is true. So let's assume that flag was false for the first time and this condition failed. Our item is equal to value and it made flag true. But this flag is true and the consumer won't know about it. Because the false value of flag is being read by cache. We saw this problem. Problem of visibility. So let's put volatile keyword here.

  42. Speaker 2

    Volatile.

  43. Speaker 1

    Something like this. And here also let's put volatile keyword. Something like this.

  44. Speaker 2

    Okay.

  45. Speaker 1

    So that it always reads from our RAM and not from our cache. Now if we run it and see, now it is printing correctly. You see, producer produced 1, consumer consumed 1, then 2 produced, 2 consumed, and so on.

  46. Speaker 2

    There is no problem.

  47. Speaker 1

    Okay? There can only be one problem here, of race condition, which may come sometimes if I run it again.

  48. Speaker 2

    Let's say.

  49. Speaker 1

    Yes, this time our race condition came. Now look, consumer consumed 1 earlier, but producer didn't produce 1 yet, didn't output on screen.

  50. Speaker 2

    How can this happen? Think for yourself.

  51. Speaker 1

    It can happen, First of all, the producer has entered. The producer has seen that our flag is false. It means that while flag is equal to true, it will not enter in the condition. It will come in this line. So, our producer's thread is item equal to value in this line. So, he must have put value in the item.

  52. Speaker 2

    Okay, very good.

  53. Speaker 1

    And our flag must have been made true. Now, as soon as he made the flag true, his contact switch happened before printing this line. Now, as soon as his contact switch happened, our consumer came. Now, the consumer saw that the flag is equal to false. There is no answer. Our producer made it true. That means he will skip this condition and will print I have consumed one value which will be the value of the item. And will null the item or will false the flag. Now when he will do all this, will switch the context and will come back to producer. Then he will print this line that I have produced item. Now what is the solution for this? By the way, the producer consumer is doing well. We have done busy waiting, which is a wrong approach. We are wasting CPU time. But now the printing time is going up and down because of the race condition. And we know that the first thing that comes to our mind is to avoid risk condition, we put synchronized keyword. So we put synchronized here as well as here. So that if we enter only one keyword at a time, then if the producer enters and that contact switch is also done, then the consumer will wait for it to come back and execute it completely. It will become atomic, which we had seen. So will our problem be solved now? Now if we run it.

  54. Speaker 2

    So what happened?

  55. Speaker 1

    Now nothing is being printed. This is a deadlock. Deadlock means that nothing is being printed and both our threads are waiting. Why would this happen? Think about it. What is the value of our flag initially? False. Now our producer can also be run and our consumer can also be run. Suppose our producer runs for the first time. So it will see that flag is false, it will enter while flag equal equal true. So is flag true? There is no answer, it will come out from here. It will enter the value of the item or whatever the value is in it, it will enter the flag as true. And it will print, I have produced a value, let's say 1. After that, the consumer will run, the flag must have become true, so it will not go in it and it will consume.

  56. Speaker 2

    This is the normal scenario.

  57. Speaker 1

    But you can imagine that in any case, if consumer runs for the first time, then our flag's value is false, consumer runs, so it comes in this loop, flag is equal to false, the answer is yes. And when it comes in, it comes in a synchronized block, that is, it took the lock of our object of this box, so it took the lock as well, and it got stuck in this infinite loop, because the value of the flag was false, and it will keep on rotating in this loop. And our producer will also never be able to enter in his synchronized method because the consumer has taken the lock and he is going around in this infinite loop. Here we have a deadlock, neither the consumer is able to move forward nor the producer is able to move forward. You will think that there is a 50-50% chance, but no, there is a 50% chance at every moment, right? At any moment, if the consumer enters from the producer first, when the value of the flag falls, then our deadlock will come.

  58. Speaker 2

    That means this solution is also not right, right?

  59. Speaker 1

    So what should we do?

  60. Speaker 2

    What is the better approach?

  61. Speaker 1

    If this busy waiting is not the better approach, then the better approach that comes from our object classes That we use the concept of waiting You know that in Java, we have a thread state in the thread lifecycle Which we call waiting state And this is different from timeid waiting in which we do thread.sleep and make it wait for some time And we said that to put it in waiting, we have a method called wait And to remove it from waiting, we have two methods One is notify and the other is notifyAll.

  62. Speaker 2

    Something like this.

  63. Speaker 1

    Now we are going to use these methods and see how we can better our thread communication. Now see, our better approach can be this, that the code we had written on top, where did our code go?

  64. Speaker 2

    Yes, this one.

  65. Speaker 1

    So I said we will do this, that if the producer has item is not equal to null, then it will keep rotating in this loop. That means if there is already a value in the item, then the producer will keep rotating in the loop.

  66. Speaker 2

    And what was written for the consumer? If there is no value in the item, then it will keep rotating in the loop. This was busy waiting.

  67. Speaker 1

    Now what happens, if I write in this condition that if the item is not equal to null, that is, if our box already has a value of 5, let's say this is our box, or this is called item, specifically this is our item, it already has a value of 5, then the producer should not be run. So I write a logic in this while loop that if the item is not equal to null, then you become wait, call the wait method. Let's understand the wait method and write the logic here that if the item is equal to or equal to null and there is no value to consume then what will you consume? You become the wait and write wait here. If I make them wait like this then they won't move in this loop. Now let's understand what is wait and how do we bring them back from the waiting state which is the notify method. First of all, if you want to communicate in a thread, then there are three main problems in thread communication. One is a shared resource, because of which there is a problem. One is the condition. Let's see what is the problem based on the condition. And the other.

  68. Speaker 2

    One is waiting. That we have to wait for a thread. So let's see.

  69. Speaker 1

    In the upper box example, what was the shared resource? The shared resource was the entire box. Or we can call it the item. This was our shared resource. And what was the condition? The condition was that box has item or not. Because if the box already has an item, then the producer should not move. And if the box does not have an item, then the consumer should not move. Now what is the waiting state? So here I can say that the consumer should wait. If there is no item, the item is empty. And on the other hand, the producer should wait. If there is an item, if the item is already there, then this should be their waiting condition. You must be understanding that our box is a shared resource. There is a condition that whether there is an item in the box or not. And what is the waiting criteria of the condition? And what is its waiting criteria? If there is no item, then the consumer will wait and if the item is already there, then the producer will wait. Now let's understand how our wait method actually works.

  70. Speaker 2

    Wait method.

  71. Speaker 1

    If you put wait method on any thread, then that thread will pause itself, whatever it is doing, right? It releases its locks, whatever it has acquired, monitor locks, and goes to a waiting state.

  72. Speaker 2

    Waiting state. Something like this.

  73. Speaker 1

    Okay? It says that the work that I was doing, I was not able to do it for any reason. So I went and paused my work. I released all my logs so that someone else can acquire the logs. And I went to my waiting state. Now, if you want to get me out of the waiting state, then another thread will call notify. Notify or notify all. There are two methods. In both of them, you must be able to understand by name. If you want to remove one thread, then notify. If you want to remove all threads, then notify all.

  74. Speaker 2

    We will see about that later.

  75. Speaker 1

    Then I will return to the runnable state and will be ready to run. So if I write definition wise what it does? It waits for 3 things. Number 1, it will release all the monitor logs. So release monitor logs. And it will go to the waiting state. It goes in waiting state. And it will stay in the same state. It stays there. Until the other thread wakes it up. Until another thread.

  76. Speaker 2

    Wakes it up.

  77. Speaker 1

    This is not a time mid waiting. This will go to the waiting state until the other thread wakes it up. Now in all of this, this point is the most important. Release monitor logs. Because if it doesn't release monitor logs, then again, deadlock-like situation will happen. Our thread will not be able to run. I will also explain that to you how. But now let's see its use. How can we use it? You think, in our producer, there was a code like this. If I talk about producer. In that we used to write, while, till the time there is already data, that means our flag, Equal equal true means data is present So you stay in this infinite loop Basically you keep on rotating I am saying this is a useless thing If we already have data And this condition is true So what I will do is I will call a method here, wait And whatever thread reaches here I will send that producer thread to waiting state So our producer went to waiting state, no problem And on the other hand, our consumer, let's.

  78. Speaker 2

    Say I write it here, this is our consumer.

  79. Speaker 1

    I will say that while we don't have data, flag is not equal to, while flag is equal to false, there is no data, then you go to the waiting state. So I called here, wait. Your consumer will go to the waiting state. So either the producer will be in the waiting state or the consumer. Because there will be data here, there will be no data here. There can't be any intermediate condition. Okay.

  80. Speaker 2

    Now if our flag was.

  81. Speaker 1

    False So basically this condition was true So our consumer was in waiting state So consumer is waiting So let's say consumer had T2 thread So I assume consumer has T2 thread It is in waiting state So producer can run So producer will not enter in this condition Producer will do his work He will put value in item And he will make this flag true Something like this And he will print that I have produced a value whose name.

  82. Speaker 2

    Is item.

  83. Speaker 1

    So this will be the scenario and the producer will come out of his method. Let's say this was his method call, he came out of it. As soon as he comes out of it, before he comes out of it, I am saying we will call another method here, whose name will be notify. As soon as I was getting out of my method, I called notify. And notify brings back the thread that is waiting for us. Which thread is waiting for us? T2. So it will bring T2 back and make it alive. Now when T2 comes back, where will it come from? From where it started running. It will test this condition again. Is the flag equal or false? The answer is false. Now the flag is true. It means that it will come out of this loop now. Now it will come out of this loop and it will consume this item. You know what code we write for this? We write item equal to null. It will print on the screen that I have consumed the item. And here it will call finally notify. So why did it call notify here? It did it because if a producer is sitting in a waiting state, who has not produced value yet, he is in a waiting state.

  84. Speaker 2

    So I have consumed it. Now any producer can produce value.

  85. Speaker 1

    So it notified that if a producer is sitting, he will come to normal state from his waiting state. So in this way, we can make an interaction between these two. Now we will see it in code, but before that we will have to talk about these two methods. First of all, our wait method and even notify method can be used only when you put it inside a synchronized block.

  86. Speaker 2

    Basically, you put it inside a lock.

  87. Speaker 1

    If you put it outside the monitor lock, then it will give you an exception. And the name of this exception is illegal monitor state exception. And this is a very important thing that why does it give you an exception? Now think, what did I say? How does wait work? Wait works in three steps. First of all, it releases its monitor locks. But it will be able to release the same lock that it has acquired. If you put this wait, suppose somewhere outside where the synchronized keyword is not used, that means it is not in any locking statement, it is not in a shared code where the locking is used. So which lock will it release? No lock will be able to release So you will get illegal monitor state exception Which means whenever you want to use wait You have to remember that you have to use it inside the synchronized block So basically our code We will have to use our synchronized keyword So synchronized void Let's say produce Our method was something like this produce Which takes a value And after that it produces it So here we will have to put our synchronized keyword Then our infinite loop was that while flag is equal equal true if flag is already true then we will call the producer wait right something like this now we can successfully put wait here okay yes so now you think if our monitor logs are not released here through this wait then what would happen so let's say initially nothing happens in our box our item is empty and obviously the value of flag is false Black's value is false, something like this. Now imagine, if the consumer enters first, then he will check this condition and it is obviously a synchronized block here because weight can't work without synchronization, it will throw an exception. So here we have to synchronize both times. And obviously we have to because we have to keep them in sync. And now the deadlock will not even come because we will try to avoid it with weight. But here the deadlock would have come if this monitor did not release the locks.

  88. Speaker 2

    How?

  89. Speaker 1

    The thread entered in the consumer is in this line. While value equal equals false. The answer is true and the value is false. So while will go to the waiting state. So our consumer thread t2 went to the waiting state. Now it went to the waiting state. But who will wake it up? To wake it up, someone has to call notify. And who would call notify? Producer would call notify. But producer can't be running now. Because it went into waiting state. And I said, if it didn't release this lock in waiting state. Then producer will never be able to enter in it. Because lock is not released. And it will go into waiting state all the time. That means we will get deadlock. But what happens if it doesn't happen? As soon as it goes to waiting state, it will release the lock on the object named box. So that the producer can enter in our critical section. Now the producer will enter. He will see if the value is true or false. The value is not true. He will skip this while. He will put a value in the item. Let's say he put a value of 10. He stored 10 in the item. He will make the flag true. We will remove this and make it true. Let's say we did it.

  90. Speaker 2

    True. Very good.

  91. Speaker 1

    And obviously, because it is synchronized till now, if its context switching happens in between, then no other thread will be able to execute it. It will do its work again. And finally it will call notify. What will happen after calling notify? The thread which was waiting will come back. And it will come out. Which thread was waiting?

  92. Speaker 2

    T2.

  93. Speaker 1

    T2 was in the waiting area. Now T2 came back here. Now the same process will repeat. Now when T2 will go inside again, it will see that the value is false and the answer is no. That means it will consume the value and null the item. It will basically store null here. And here it will store false again And will notify Now who is it notifying? If the producer is waiting for it But when will the producer be waiting?

  94. Speaker 2

    The producer hasn't.

  95. Speaker 1

    Waited yet You think As soon as T2 came here from the waiting area T1 is ready to execute again Because I might have executed it in the loop If T1 gets active again to produce and the value is not consumed yet and T1 acquires the lock again, then the value will be equal to true. So our producer will go to the waiting area. If this is a waiting area, then T1 will come here. And if T1 comes here, then this will also have to notify someone. Consumer works to notify this. Consumer says, first let me occupy the value or consume. Let the flag be false. Then I will notify and you go back to the jug. So this work will continue continuously. No one will disturb anyone. No deadlock will come. Because our producer and consumer will release their monitor locks. As soon as they wait or notify call. I hope you understood how wait works and you must have got the idea of notify. Now let's understand the internals of this. I told you that in java, every object has an intrinsic lock. You saw about this in your last class. If you haven't seen that lecture then do watch it. Every object has an intrinsic lock. This is called monitor locks in java. So let's say I write the name monitor locks. Now, there must be some information in these Monitor Locks. There must be some information like which is the owner thread? Who has occupied this lock?

  96. Speaker 2

    Right?

  97. Speaker 1

    There must be a lot more information. Apart from this, there is one more information called Waiting Queue. Waiting Queue. Now what is this waiting queue? In the last lecture we saw that there must be a blocked queue in which those threads which are blocked go. In the same way, there must be a waiting queue in which those threads which are in the waiting state, so you think that we had two threads, one thread was T1 and one thread was T2. T1 runs the producer and T2 runs the consumer. So if we assume that T2 is on our weight, then basically what will be T2? T2 will come inside this, it will be standing in the waiting queue. And you know about producer and consumer, both of them have the same object box method. So either the producer can take the lock of this object box, i.e. T1, or the consumer can take the lock, i.e. T2. Now if we assume that the consumer took the lock of this, so the consumer took the lock of the box, then the information in the owner thread will be T2. And after taking lock, it started running. And suppose it went to waiting state. We had discussed here that if the consumer has occupied lock. And in this while, value is equal to false. Suppose the answer is true, initially value is false. So that became wait. So as soon as our consumer goes to waiting state. In this waiting queue, T2 will come. T2 came here.

  98. Speaker 2

    And what did I say?

  99. Speaker 1

    As soon as someone comes to waiting state, he releases his monitor locks. Right. First, let's say it gets stored in the owner thread. because the owner thread was T2 and as soon as it came in waiting state, it released its monitor locks which means T2 has been removed from here and null store has been done here that there is no owner thread Now as soon as it released the lock, our producer can enter again Because till the time producer couldn't enter, the consumer was already in And producer couldn't enter because he was in the last class Because he was in a blocked queue You know there is a blocked queue in which those elements go If they are trying to occupy a lock, then they are also blocked They are blocked means they are not able to occupy the lock Because no one has released the lock yet But as soon as T2 came in the waiting queue, he released the lock Now producer will return from the blocked queue to the runnable state And it can start running from the runnable Now I hope you are getting the connection of how all the states are connected. Now our second method is that if someone is in a waiting state, then who will wake him up? To wake him up, there is another method called notify. And where do we apply this notify method? We apply it in both, obviously in the producer and in the consumer. Because both of them wake each other up. Now let's see what notify does. As soon as we use our notify method. Now whenever a thread calls notify, it says that whatever object we have, the waiting queue in which there are many threads in waiting state, wake up one thread randomly from all those threads. For example, in this case, which we saw above, if we would have called notify, let's say in producer or consumer, anywhere, then what would it do? It would go to the waiting queue, see which thread is waiting. In our case, only one thread is waiting. So wake up this thread. Wake up means, Remove its name from the waiting queue. Right?

  100. Speaker 2

    Now it is not waiting. And put it back. In which state? Put it back in the blocked state. Remember. Many people think in the runnable state. Okay? That is a little wrong answer. In the blocked state.

  101. Speaker 1

    Why? Because if it comes out of the waiting queue once, then it will have to acquire that lock again. Because someone had released the lock. Right? Now I will explain it through an example.

  102. Speaker 2

    This was our code.

  103. Speaker 1

    Okay?

  104. Speaker 2

    Initially, this was the design. T1 thread, T2 thread. And initially, the value is here.

  105. Speaker 1

    True.

  106. Speaker 2

    And 10.

  107. Speaker 1

    Assume. initially we have no value here, so here also false will be there, and null value will be there, so let's say t2 runs, t2 sees whether the value is equal or false, the answer is true, the value is false, so t2 runs in the waiting state, so let's say this is our waiting queue, and here comes t2, In this waiting queue.

  108. Speaker 2

    This is our queue.

  109. Speaker 1

    Now remember, this Q will be in respect to an object. This producer and consumer both are part of the same object. The object's name is box. So I'll show it here. Both are part of the same object. So this box has a Q. It has T2 in it. Now think, T1 comes to run. Does it see if the value is equal or true? The answer is no, the value is false. So it puts a value in the item. Let's say it put a value of 10 here. This false is called true. Here it has stored let's say true. Something like this.

  110. Speaker 2

    And calls notify. Now, what does notify do?

  111. Speaker 1

    Notify says to remove this T2 from the waiting queue and put it in a blocked state.

  112. Speaker 2

    Now I will tell you why I said blocked state.

  113. Speaker 1

    First of all, we removed T2 from the waiting queue and put T2 in a blocked state. The reason is that as soon as T2 waits, it will release the synchronized lock. That's why our producer was able to enter the synchronized block. So if it releases the lock, then how can it execute this statement again? How can it come back to this block? It will have to acquire the lock from the start. So T2 is again involved in the process of acquiring the lock. As soon as it gets the lock, that is, someone else releases the lock, maybe the producer hasn't taken the lock yet. So as soon as the producer notifies and comes out, the lock is released. Now our consumer or T2 thread comes in the runnable state. Runnable. Now look at the interesting catch here. It has come in a runnable state. As soon as it gets CPU time, it will come in a running state. Now where will it start running? Will it start again from the beginning? The answer is not at all. From where it stopped, it stopped from this line. So it will start running again from this line. So here it stopped from wait. And because it is a loop, it will go back to the loop. Now see if the value is equal or false. The answer is no, because our t1 has made it true. So it will come out of this loop. It will consume the item. Item equal to null, flag equal to false. Right? So it consumed the item from here. It made the flag's value false again. False. Something like this. And then it called notify again. Why did it call? Because if t1 is in wait state, In case T1 would be here, then it would be notified, i.e.

  114. Speaker 2

    It would wake up. Something like this.

  115. Speaker 1

    We just have to remember that whenever a queue comes out of the waiting state, it always comes in the blocked state. And the reason for this is very simple. Because it had released its log, it needs to take its log again. So it will go to the blocked state. If no one has occupied the log again, then it will come to runable. And as soon as it gets the CPU time, it will start running. And where will it start running? From the line it left. Okay. I hope you understood this. But interestingly, do you know what I said about notify? That it wakes up a random thread from all the threads in our waiting state. So let's say I can write wake up a random thread.

  116. Speaker 2

    From waiting queue.

  117. Speaker 1

    If I write its pointers, For example, we had written the wait pointers on the top.

  118. Speaker 2

    Wait does these three things.

  119. Speaker 1

    It releases the monitor locks, goes to the waiting state, and stays there until the notify calls. Similarly, if I write the notify pointers, then first of all, one random thread is picked from the waiting queue. After that, that thread moves to the block state. And if it is in the block state, it will compete again for the lock. Compete for the lock. And if it gets the lock once, Once lock occupied, it will come back to its running state, which you can call it runnable or running, both are in the same state in java. And it will start executing its code. Now the most interesting point here is, pick one thread at random. It picks a random thread, right? That means again, this is very non-deterministic. you can pick any thread because you have only called notify it depends on our cpu and operating system which thread will wake up Another point is that notify should also come in a synchronized block. Because notify can wake up any thread in the waiting queue. So, this should also be in synchronized. If you write it outside of synchronized, then again what will happen? This will throw you an exception. The same exception will throw you, which throws you weight. Whose name is illegal monitor state exception. You already know this. Okay. Now, using weight and notify, let's write our entire code again. So here I have copied my old code in demo3. java Same I have made a box class here In which there is an item and a flag Here I was writing do nothing And here I had put synchronize in both in the last In which our deadlock came So now here I will write instead of do nothing If the flag is equal equal true Then I will put wait So I will tell the producer to wait Now obviously wait gives a checked exception Interrupted Interrupted exception, we know this, everyone gives this So let's do one thing, let's throw it here Interrupted exception So here I have asked to wait. Whoever comes here, the producer's thread will come here. So it will execute flag = true. If the answer is true, basically if there is already a value in the item, then we will ask the producer to wait. That means go to the waiting queue. Now, who will take it out from the waiting queue? The consumer will take it out.

  120. Speaker 2

    When will he take it out?

  121. Speaker 1

    If the flag's value is true, that means the flag's value is not false, that means flag equal equal false will be false, that means the consumer will come in this line, he will consume something, whatever is basically written in the item, he will get null store in it, he will false the flag and finally call notify.

  122. Speaker 2

    Okay? Now what will notify do?

  123. Speaker 1

    The thread that was waiting here will be brought back to the block state. And as soon as it ends, the consumer who has acquired the synchronized lock will release the lock. Our producer will come from the block state to the runnable state. As soon as he gets the CPU time, he will start running. Now the flag will be false because the consumer has consumed it. And he will put a new value again, he will make the flag true and print it. Now let's assume that before the producer runs again, the consumer runs again as soon as it ends. In that case, flag = false, will be true because it has just taken out the value. And instead of doNothing, we will write to the consumer that you also wait.

  124. Speaker 2

    Again, I can write throws here as well. InterruptedException. InterruptedException. Okay.

  125. Speaker 1

    Here I will say wait. Now who will notify this? Producer will notify this. Because if flag is false, means flag is not true, means value has to be entered in flag. So as soon as any value will be entered in item, flag will be true. It will print that value. And again call notify. Okay. So you can see wait notify is also here. Wait notify is also here. Both are raising each other and both are asking each other to wait. Now if I run this code, first let's see what's happening here. So I have made two threads here, T1 and T2. I am stopping the first thread for 100 milliseconds and running it 20 times. I am stopping the second thread for only 70 milliseconds and running it again 20 times. Now since I'm using synchronize and volatile keyword both times And I'm also using wait and notify So basically my CPU time is not getting wasted either There's an error here, which is unhandled exception Yes, because we've done throws here So we'll have to handle the exception somewhere Now to handle this exception, let's put this in the try catch block which is already made So I'll put it here And I'll also put this in my try catch, which is here Because.

  126. Speaker 2

    Both are throwing the exception.

  127. Speaker 1

    Okay So here I don't think I should have a producer-consumer problem If I run it You will see that my code is working perfectly No problem Producer produces something, consumer consumes something Producer produces something, consumer consumes something It will never happen that consumer consumes something empty Or producer produces something twice If you want, you can make the milliseconds of both of them equal Let's say I make this speed to 100, no problem If I run them then also same output will come, first producer will run, then consumer will run, then producer will run. Because our code will never allow the consumer to run first, it will bring the consumer in wait if the consumer runs first. Or it will not allow the producer to run twice at a time. So now our code is completely thread safe, because we have used wait and notify here, so we are not wasting CPU time here, both threads wait for themselves, they can wait as long as they want, until they get notified. Now if you understood notify, then let's understand our second method in the last, which is notify all. It must be able to understand from the name itself What it will say is that all the threads in our waiting queue Notify all of them, that is wake up all of them That means if I write its pointers So these will be its pointers which are very simple You know what notify all should do All threads in waiting queue are moved to the blocked state All the threads in all the waiting queues It will tell them to wake up and go to the blocked state All of them will try to acquire the lock Only one of them can get the lock So whoever gets the lock will start their execution Only one thread gets the lock And whoever gets the lock will start the execution. It's a normal thing. We could have used notifyAll in our method. Obviously, notifyAll was only for one thread. But I'm telling you, notifyAll is a safer method than notify.

  128. Speaker 2

    And why is that?

  129. Speaker 1

    I'll explain it to you. We generally prefer to use notifyAll. Even though it's a bit slow, you must be getting it. Its overhead will be more because it wakes up all the threads. So obviously, its overhead will be more. But now let's understand how it's safe and how notify is unsafe. Yes, so how can notify be dangerous? To explain that, I took the same code. I have a producer, I have a consumer and I have used wait and notify as expected. Okay.

  130. Speaker 2

    So now let's consider a scenario.

  131. Speaker 1

    Till now we were assuming that we had a thread of one producer T1 and a thread of one consumer T2. But again, there can be many producers and many consumers right? Multiple producers can put value in one box and multiple consumers can acquire value from it. So the condition will be the same that in our object box, there is one flag And there is an item in it. This item is an integer so it is going to be unique. So if we have multiple producers, let's say I call them P1, P2, P3 and so on. It can be any number, it can be multiple consumers, C1, C2, C3 and so on. One producer can add value in one bar. If a producer adds value in one bar, then the other producer or the same producer can't add value again. It will go into waiting state. And if there is value in the same bar, then one consumer can consume it in one bar. Once the value is consumed, then no other consumer can consume it again. It will go into waiting state. Okay, so let's move ahead with this example. Let's say I have two producers. P1 and P2 and in the same way let's say I have two consumers C1 and C2 something like this and let's also make a waiting queue so let's say this is our waiting queue okay let's make it a little smaller now let's understand it carefully for example initially our flag was false and there was nothing in our item so I left it empty First of all, let's assume our consumer enters C1. So as soon as C1 enters, it will see this condition, while flag = false. Is the flag false? The answer is yes. So it will go to the waiting state. So our C1 has come to the waiting state. Let's say C1, I'll write it here. As soon as C1 came to the waiting state, it moved away from here. And it also released the synchronized log that it had acquired. Now as soon as it released the lock, it released the lock of our box. Now in this box object, we know that there is a lock. It has a lot of details like a map. It can store the current thread that I have acquired. So let's say by default, C1 acquired it. Now C1 left it and went away. So its lock is null. That means anyone can acquire this lock.

  132. Speaker 2

    P1, P2 or even C2.

  133. Speaker 1

    So all three will fight to acquire the lock. Suppose C2 gets the lock this time too. So the consumer got the lock again. So it will come again, while flag equal equal false. The answer is yes, true. So it will go back to waiting state. So our C2 also came here. So C2 also left here. As soon as C2 again came in waiting state, it also released the lock. Now, who is the contender to acquire the lock? It's P1 and P2. No consumer is left because no value has been produced yet. So, let's assume P1 enters this time. It means P1 has acquired the lock. Is the flag equal to true? The answer is no. It skips this condition. Item equal to value. Let's say someone inserts a value. I'm saying we insert 10. So, we put the value in the item. We made the flag true. And we called notify. Now you know what notify does, it wakes up only one thread. So let's say C1 wakes up from C2. So I can say C1 came back from here and it left from our waiting queue. And when C1 wakes up, P1 had notified his work and left. And as soon as P1 notified, P1 released the lock. So we remove p1 from here because p1' s work is complete. I am assuming that one thread will work only once, it will not loop again and again because obviously we already have multiple threads. So p1' s work is over, it has notified and gone, c2 is in our waiting queue and c1 is active. Now, As soon as P1 has completed its work, it will have released the lock. And it will be notifying C1 as well. Which are our contenders now? We have C1 and P2. One of them will acquire the lock of our box object. If C1 acquires it, it will go to the consumer. If P2 acquires it, it will go to the producer. Now, let's assume that producer gets a lock and this is a critical moment. As soon as P2 gets a lock, he comes in and asks if the flag is true. The answer is yes. So P2 is in our waiting state. So let's assume that P2 is here. As soon as P2 went to waiting state, it released its lock.

  134. Speaker 2

    As soon as it.

  135. Speaker 1

    Released its lock, the only one left to acquire the lock is C1. As soon as C1 got the lock, do you know from where C1 will start its execution? C1 will start its execution from here because C1 has already come from the waiting queue. You know C1 went to the waiting queue from this line and when he came back and acquired the lock, he will start the execution from this line. So it will go back to the loop and see if the flag is equal or false. The answer is no, the flag is true. So it will skip it. It will store null in the item. Basically, it has consumed the value. It will do false for true. You can assume that it is printing in the middle. We have skipped it now. And then it will call notify. Now, this is a critical moment, as soon as C1 completed his work, C1 left from here, let me remove this, C1 completed his work and left, and he called notify, that means any one of these two will wake up, but now you think, if C2 wakes up, then what will happen, okay, so let's assume this time randomly C2 wakes up, so C2 leaves from here, okay, and C2, And P2 stays in our waiting queue Now as soon as C2 wakes up, what will happen? C2 is the only contentor of the lock It will come back in and see if the flag is equal or false Obviously it will start the execution from here It will go back to the loop and see if the flag is false So it will go back to the waiting state Which means C2 went back to the waiting state As soon as it was alive, it went back to the waiting state Why did it go back? Because it went by doing false Now C2 is also in the waiting state, so I will make it sit here Now see what is happening. Our C2 is also in waiting state. P2 is also in waiting state. And no one can wake up anyone. Right? Because both can wake up each other. And where are both of us? In our waiting queue. That's why notify can be very dangerous. Right? I hope you understood. What is its solution? Its solution is that we use notify all. Because notify all will wake up all the threads at the same time. Okay? So in this case also, see what happened in the last. Our P2C2 was in the waiting area And I said As soon as C1 completed his work He waked C2 But if we had used notify all here Let's say I would have written here Notify all And everywhere I would have written Notify all. Notify all is safe because C1 completes his work and asks them to wake up. So P2 is also wake up here and C2 is also wake up here. Now they are fighting for thread again. Our waiting area is empty. Something like this. Now this is safe because both are back. Now if C2 acquires, then it will go to the waiting area, then P2 will come. So basically someone or the other will keep performing the work actively here. And there will never be an issue because of notify all. Because if everyone is active and if the wrong side tries to run, then it will go back to the waiting state.

  136. Speaker 2

    No problem.

  137. Speaker 1

    But by activating everyone, we will get the benefit that the deadlock-like situation will never come, which we just saw. I hope you understood when to use notify and when to use notify all. Now we are sure that notify should be used only if you are sure that you have one thread of each type. I have one thread of producer and one thread of consumer, there will be no issue. But if there are multiple threads, then always go for notify all. Yes, it is a bit expensive because it has its own overhead. The overhead is to remove all the threads from the waiting list. But this is very safe. This is our error prone. I can say that this is our deadlock prone. Because of this, sometimes it can be deadlocked. Now, here comes a very interesting interview question. These three methods, one is your wait, one is your notify, And the other one is notifyAll. These three methods belong to the object class. But these should be the methods of threads. Why do they belong to the object class? The reason is very simple. When a thread acquires a lock, it acquires a lock of an object. So to do inter-thread communication between them, we need these three methods, wait, notify, notifyall. So obviously, this should also be in the object class. Because in the class where there is a lock, in the class where there is a waiting area, right? So notify also takes people out of that waiting area. And wait puts people in that area. So these three methods should also be there. Obviously, in our object class, where there is a lock, right? Okay, I hope you got this clear. In fact, the most interesting question is, what is the difference between sleep and wait? You must have got this clear. Sleep works by taking you to a timed waiting state. Timed waiting state, we saw this. It makes you wait for a certain time, right? Like we write sleep for 200 milliseconds. And wait takes you to a normal waiting state. And it can make you wait irregularly until you get a notify call, right? Now look, If you understood all this, let me tell you something interesting. If we go back to our previous code, like here, even though if you ask a thread to wait and it goes to the waiting area, then this is not a 100% guarantee that it will not come out of that waiting area by itself. Right? Sometimes it can happen that you don't even notify or notify all call and it comes back out of the waiting area in its blocked state or in its runnable state. This can happen. And this phenomenon is specifically called spurious wake up. Spurious wake up. This is very rare but it can happen. Now why can it happen? Again its answer is the same that java can't fully control our threading. Who controls our thread? Our operating system plus our CPU. So because of their optimizations, they must have felt that this thread has been in the waiting area for a long time. Because of their optimizations, where do they bring that thread back from the waiting area? In their blocked or unable state. And all our work can go wrong. If we had written wait, we told them to wait. So all the work can go wrong. This moment is called spurious wake up. And to avoid this, we follow a technique which we were following from the beginning. Right?

  138. Speaker 2

    So I'll show you what that technique is.

  139. Speaker 1

    Look, whenever we used to write our code, think about it, we always used to write while flag = = false. Think about it, why didn't we write IF instead of WHILE? Same here, why didn't we write IF instead of WHILE? Even IF checks if the flag is false. If it is, then wait, if not, then don't. We didn't write it because if by mistake a spurious wake-up occurs, so let's say there's a C2 thread here that I wanted to wait for, but it woke up by mistake. So when it will wake up and start implementing again, then from where will it do it? From this line from where it left the execution. And because this is a loop, it will check the condition again.

  140. Speaker 2

    Is the flag false?

  141. Speaker 1

    And because if it wake up irregularly, then the flag may be false. So it will go back to its waiting area. Now you will say, if the flag is true, then it is okay. Then it would have been notified and wake up. If this flag had been true, So the producer would have notified him But the producer hasn't notified him yet And it has wake up completely, which means by sure The flag here was false It woke up on its own, we didn't want it So it's okay, we'll make it sleep again We'll send it back in the loop, if the flag is false then go back to the waiting state So this while condition is very special Many times in the starting we write if flag.

  142. Speaker 2

    Equal, equal, false.

  143. Speaker 1

    And that's why we didn't write it from the starting. If we had written if here, then what would have happened? Then what would have happened? If it had waked up and came back, then it wouldn't have gone back to the loop. There is no loop, there is only this statement. So it would have skipped it and come to the next line. And our irregularity would have come. That means it is possible that we have a flag false, that item something happened. Even then our consumer C2 came back. And it started reading this item. Basically, it started reading the null value. Okay. So, this shouldn't happen. This complete wakeup problematic shouldn't happen for us. That's why we always use while. While for any condition. Let's say flag equal to... Let's say flag = true, or = false, or whatever. In a condition where we are waiting for a thread, we always use while on top, so that when it.

  144. Speaker 2

    Returns, it will check its condition again.

  145. Speaker 1

    That's why in Java, when we write while, it has a special name, we call it guarded block.

  146. Speaker 2

    Basically, I have guarded my block.

  147. Speaker 1

    No matter what happens to any thread, even if it wakes up by mistake, it can't leave my block and go out. Okay?

  148. Speaker 2

    I hope you understood.

  149. Speaker 1

    Now, this producer-consumer problem that we discussed, producer-consumer problem, if you think of it from a real-life scenario, then if you assume that If a document creator is a producer, then a printer can be a consumer.

  150. Speaker 2

    He prints that value. If you give something, he will print it.

  151. Speaker 1

    Same with any real-life scenario, in which you are producing a value and a value is consumed from somewhere. All of that lies in the producer-consumer problem. And this is a classic problem of multi-thread. And we have seen its solution today. I hope you enjoyed it. You must have learned a lot. See you in the next video. Till then, thank you so much.