13.8 Multithreading InterThread Communication | Producer Consumer

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 146

  • @ahmadazeem7785
    @ahmadazeem7785 5 місяців тому +4

    There are so many tutorials on Multi Threading but I found your explanation as the best one.

  • @karthee_k
    @karthee_k 8 років тому +56

    Can't explain simpler than this. All videos on thread are great.

    • @dragonrojude5496
      @dragonrojude5496 7 років тому

      hi can u expalin the working logic of while statements in class q class.
      thank u

  • @himanimishra5893
    @himanimishra5893 7 років тому +12

    youre an excellent teacher. After watching some 10 videos I know for sure Ill learn java.Earlier I used to hate programming coz I didnt understand properly.Thankyou for increasing my Interest sir. Great teaching . :-)

  • @krishnabangari9287
    @krishnabangari9287 Рік тому +3

    Subscribed and will surely donate once i get my first salary. This guy deserves it for his work.

  • @Mukund_Pandey
    @Mukund_Pandey 4 роки тому +1

    seedhi baat no bakwaas! Great Explaination.

  • @preethamm.n1161
    @preethamm.n1161 5 років тому

    One of best vedio on producer and consumer on UA-cam !!!
    Thank u sir💞💞💞🌹🇮🇳🇮🇳🇮🇳🇮🇳

  • @vishaltanawade7637
    @vishaltanawade7637 4 роки тому +9

    Only we have to use synchronised method Instead of doing other hard stuff ... Try

    • @548vishnureddy6
      @548vishnureddy6 3 роки тому

      Nice bro

    • @ashishwatkar8275
      @ashishwatkar8275 3 роки тому +1

      No, it's not, we have to use synchronization only when we want that only one thread should execute at a time. So in synchronization one thread will execute at a time but which thread will execute we cant give a guaranty. Now in the Producer consume problem it is mandatory that our producer thread must complete its execution before the consumer, but by simply using synchronized keyword we cant achieve that, that's why we need to use wait(), notify(), and notifyAll() here.

    • @Comment975
      @Comment975 9 днів тому

      No

  • @satyendrasingh-oh7mp
    @satyendrasingh-oh7mp 6 років тому +11

    very great explanation.. but i have a doubt why do you use while(setValue) instead we can use if(setvalue) ??? rply

    • @ManishKumar-qx1kh
      @ManishKumar-qx1kh 4 роки тому +4

      if wait is called on some thread and notify is not called then thread might become runnable again after some time , so its better to use while loop and call wait inside that so that even if thread wakes up it can check the condition and go to sleep again if the condition is not met. Here i belive u can use if because notify is called and time lapse is not that much.

  • @akashkarthik7892
    @akashkarthik7892 6 років тому +4

    All videos on multithreading was great and understandable but this one is very confused I slept watching this video woke up now

  • @N63B44TÜ
    @N63B44TÜ 5 років тому +14

    It was great!
    Some people want to get everything ready, so why are you programmer?!
    It was really helpful thx!

  • @karishmasukhwani5270
    @karishmasukhwani5270 3 роки тому +14

    Navin Sir, thank you so much. You know, juggling up with so many threads threads in my JavaFx pong game. I wish I had someone like a mentor to me in Java. Your videos are there but I don't know how can I connect you if I get in to some problem which I can't find over google. Still, thank you so much for your videos. Half of Java and Python, I have learned from you.
    Waiting if you have some forum where I can connect you.

    • @Aditya-sn4le
      @Aditya-sn4le 8 місяців тому

      Try connecting on Linkdin or his website

  • @sureshchaudhari4465
    @sureshchaudhari4465 2 роки тому

    Multithreading beyond theory waw telusko

  • @prateekbhatnagar9852
    @prateekbhatnagar9852 2 роки тому +1

    Hi , Didn't understand the usage of notify function . i tried to run the program without the notify() function and it runs properly .

  • @surajkudale5128
    @surajkudale5128 6 років тому +9

    Little bit confusing but still manageable. Other videos are great

  • @jatinlalwani9459
    @jatinlalwani9459 7 років тому +67

    All this can be done without the use of wait() and notify() and synchronized(). why do we have to use all this? Also you were not able to properly explain how notify works and why synchronized is used. Poor explanation!

    • @mouneshvannur6675
      @mouneshvannur6675 5 років тому +2

      Cause he is explaining without blockingQueue..

    • @jyotsnavootla2138
      @jyotsnavootla2138 4 роки тому +9

      Sweety, ever heard of constructive criticism?

    • @Saifullah-su8ti
      @Saifullah-su8ti 2 роки тому +1

      Actually synchronized key word gets the object lock. Research it and it would be clear

    • @Saifullah-su8ti
      @Saifullah-su8ti 2 роки тому

      Wait releases the object lock and notify keyword enters the thread in entry set so that it can acquire object lock after the lock is released by the thread

    • @inxeoz
      @inxeoz 2 роки тому

      @@Saifullah-su8ti thanks man

  • @Moe.firouzabadi
    @Moe.firouzabadi 5 років тому +22

    if you guys know all of these, then what the hell youre doing here?

  • @chelovekgrey1249
    @chelovekgrey1249 8 років тому +7

    what if we have 3 threads t1,t2 and t3. t1 and t2 are waiting and when t3 complete its task we need to notify thread t1.. not thread t2... how it will be done...??

    • @revanthmadasu244
      @revanthmadasu244 7 років тому

      reply if you find answer for this

    • @aarushverma8432
      @aarushverma8432 7 років тому

      use notifyAll( );

    • @chelovekgrey1249
      @chelovekgrey1249 7 років тому

      Aarush Verma notifyall() will let all thread in process. but I want to notify just 1 thread , not all??

    • @piyush12121
      @piyush12121 7 років тому +1

      Only one of the thread will be able to get into the synchronized method when you call notifyAll(). Which one ? Whichever has the highest priority. If all have same priority then you can't say exactly which one will get into its method.

    • @navjotkaur4795
      @navjotkaur4795 7 років тому

      NotifyAll() will solve this problem. It has same functionality as notify but it will notify all other threads instead of only one

  • @yatharthrathore1117
    @yatharthrathore1117 7 років тому +3

    Can you implement dining philosopher problem using threads.

  • @ShermukhammadKarimov
    @ShermukhammadKarimov 7 місяців тому

    thanks for clear explanation

  • @andreslebon3869
    @andreslebon3869 7 років тому +4

    But, it does the same job just with the 2 synchronized put() and get() methods, without the need of wait() and notify()..I did not get the point.

    • @hariprasadreddy108
      @hariprasadreddy108 6 років тому +2

      no, Synchronized method is to avoid other thread to call same method suppose if t1 and t2 are producer threads , lets say t1 calls put method and went to wait state but in the mean time t2 thread comes into picture and calls the put method again so in that case producer method produce new value even consumer thread say t3 not consume earlier value ,so the conclusion is to avoid different threads t1 and t2 call same method we use method synchronization

    • @shanmukhishammu267
      @shanmukhishammu267 5 років тому

      did you get the answe ?? cz my comment is same:(

  • @supriyagorai9483
    @supriyagorai9483 5 років тому +2

    Please add some videos on Thread pools, deadlock and daemon lock..

  • @pavithrasree4303
    @pavithrasree4303 6 років тому +5

    Can we get a video about thread pool?

    • @ManishKumar-qx1kh
      @ManishKumar-qx1kh 4 роки тому

      u can check video of Defog Tech. Its a pretty good channel consisting videos of multithreading high concepts

  • @dragonrojude5496
    @dragonrojude5496 7 років тому +2

    can someone briefly explain the logic line by line from the start because i am a bit confused with the program when it reaches the while statements i class Q

  • @vinaysingh2971
    @vinaysingh2971 3 роки тому

    excellent sir...well explained

  • @aravindhvellaiyan2300
    @aravindhvellaiyan2300 3 роки тому +1

    We could achieve this same logic even without using wait() and notify() by using this valueSet check, they why should we go for wait() and notify() here.?
    Am I got anything wrong.??

  • @sagar1190
    @sagar1190 4 роки тому

    Very good example for learning this concept. Thank you Navin !!

  • @kathireshkumar5793
    @kathireshkumar5793 5 років тому +1

    Thanks for good session

  • @dinkarinjosh
    @dinkarinjosh 5 років тому +7

    you seem to be confused.

  • @anshulgupta5204
    @anshulgupta5204 5 років тому +1

    What is the best way to study RX Java? Any sugestions

  • @bichitrasahoo4943
    @bichitrasahoo4943 7 років тому +1

    Sir, u have explained it very nicely.

  • @deepakrawat5065
    @deepakrawat5065 7 років тому +1

    Beautifully explained.Thank You.

    • @dragonrojude5496
      @dragonrojude5496 7 років тому

      hi can u explain the working logic of while statements in class q class. thank u

  • @bharatarora640
    @bharatarora640 6 років тому +1

    Really good job.. thnx fr the videos..

  • @davidakful
    @davidakful 7 років тому +2

    It does not work as expected. Even when i don't have notify(), the program is running. And get is before put.

    • @aronblanche
      @aronblanche 4 роки тому

      what java version you're running?

  • @abhishekgairola9166
    @abhishekgairola9166 10 місяців тому

    Incase of multiple producers and consumers , how does notify() method exactly work ?
    Does it notify all threads ?

    • @harshwardhanshetty4683
      @harshwardhanshetty4683 10 місяців тому

      it notify() only one thread which is waiting for resource to release a lock in this case sync function aquires lock while wait() releases lock on resource which is Q and waits itself for other to use it the other thread gets notified that there is release in resource you can use it first it aquires lock on resource then then same waits itself the sir is really confused here and he is mugging up concept of sync i think maybe i am wrong but from video he looks bit confused because there is also no need of other boolean variable if you can change the logic

    • @harshwardhanshetty4683
      @harshwardhanshetty4683 10 місяців тому

      for ur clarification see this code
      public class ProducerConsumer {
      public static void main(String[] args) throws Exception {
      Q lock = new Q();
      Producer p = new Producer(lock);
      Consumer c = new Consumer(lock);
      p.start();
      c.start();
      }
      }
      class Q {
      int i=0;
      void setI(int num){
      this.i=num;
      }
      int getI(){
      return this.i;
      }
      }
      class Producer extends Thread {
      Q lock;
      Producer(Q lock) {
      this.lock = lock;
      }
      @Override
      public void run() {
      try {
      synchronized (lock) {
      while (true) {
      lock.setI(lock.getI() + 1);
      System.out.println("Producer produced -> " + lock.getI());
      Thread.sleep(2000);
      lock.notify();
      lock.wait();
      }
      }
      } catch (Exception e) {
      }
      }
      }
      class Consumer extends Thread {
      Q lock;
      Consumer(Q lock) {
      this.lock = lock;
      }
      @Override
      public void run() {
      try {

      synchronized (lock) {
      while (true) {
      System.out.println("Consumer consumed -> " + lock.getI());
      Thread.sleep(1000);
      lock.notify();
      lock.wait();
      }
      }
      } catch (Exception e) {
      }
      }
      }
      the main important thing here is not to control the function but acquiring and releasing lock on resources i was interviewed by oracle and somewhat felt humiliated by not understanding concept in depth for mutlithreading

  • @sahanagowda9123
    @sahanagowda9123 Рік тому +2

    All other explanations are good but this i really din’t understand

  • @AbhigyanGautam
    @AbhigyanGautam 5 років тому +2

    Thank you so much
    It cleared everything

  • @akshatsingh6036
    @akshatsingh6036 4 роки тому +1

    sir can we do that like adding sleep(10) in between new Producer and new Consumer for this output

    • @ManishKumar-qx1kh
      @ManishKumar-qx1kh 4 роки тому +1

      yes u can use. It will work i=even if u dont call sleep. Here wait and notify are main concepts.

  • @shanmukhishammu267
    @shanmukhishammu267 5 років тому +3

    even without wait and notify methods ,i am getting the same expected output with just synchronized get and put methods
    can u please explain why??

    • @ninadsentinel
      @ninadsentinel 5 років тому

      shanmukhi shammu You have basically made Q threadsafe by synchronising both put and get. This synchronisation is governed by intrinsic this lock so at a time only one of the threads can execute either of the two methods. I hope this clears.

    • @madhavanupoju3324
      @madhavanupoju3324 4 роки тому

      In Consume class, increase the sleep time then you will understand and test without calling wait and notify methods

  • @arihantjoshi7003
    @arihantjoshi7003 5 років тому +1

    Is the source uploaded somewhere?

  • @asian-series7071
    @asian-series7071 7 років тому

    100/100 marks sir bahut accha

  • @lionciomorcilla4743
    @lionciomorcilla4743 6 років тому +1

    thankyou very much!! now i solved my problem

  • @ramkumar_balu
    @ramkumar_balu 3 роки тому

    The wait and notify logic should come inside the producer and consumer thread instead of Q class. is not it? Those logic shuold not be controlled externally.

  • @danipr100
    @danipr100 3 роки тому

    I think that this is wrong, the synchronization is happening because of synchronize key word not because wait/notify - one thread doesn't have reference to the other. high risk of dealock.

  • @naeemkirmani9934
    @naeemkirmani9934 5 років тому +4

    always make concept very difficult i dont like big big example for small concept.

  • @prashantkamble898
    @prashantkamble898 8 років тому +1

    Very good explanation. Thank you.

  • @premalupadhyay3555
    @premalupadhyay3555 3 роки тому

    Sir, I have a question that if we call the sleep() method then the thread will go to waiting state?

    • @HimanshuSingh2009
      @HimanshuSingh2009 3 роки тому +1

      The thread will not go into a waiting state, the sleep method stops the current thread execution for a specific time.

    • @premalupadhyay3555
      @premalupadhyay3555 3 роки тому +1

      @@HimanshuSingh2009 thanks 👍

  • @learnandexecute
    @learnandexecute 6 років тому

    not sure why, the code doesn't works at my end. this particular line gives error "Thread t = new Thread(this, "Producer"); " If I remove "this" keyword the error gets solves in eclipse but code does not RUN, after that error message is: Error: Could not find or load main class InterThread
    ''
    I tried to fix the problem referring to the book called JAVA.the.complete.reference but couldn't solved.
    Please check if anyone can help
    .

    • @HussainMohammedAshruf
      @HussainMohammedAshruf 6 років тому

      this keyword cannot be used within static method. Make sure it is not static to use this.

  • @revanthmadasu244
    @revanthmadasu244 7 років тому +1

    how producers notify() notifies consumers thread? how are they're connected to each other?

    • @saurabhgupta6080
      @saurabhgupta6080 3 роки тому

      notify() simply wakes up one of the threads waiting on the same lock, and notifyAll() method wakes up all the threads waiting on the same lock.

  • @asifsyed1594
    @asifsyed1594 6 років тому

    This concept works without wait and notify. Also one more point here is that we need to use synchronized for notify method, not wait method.

    • @kathiresan_n
      @kathiresan_n 5 років тому +1

      Do u think wait method can b called out of synchronized block....? Pls refer this "The wait(), notify(), and notifyAll() methods should be called for an object only when the current thread has already locked the object's lock. This point sometimes goes unnoticed because programmers are used to calling these methods from within synchronizedmethods or blocks. Otherwise, you will get "java.lang.IllegalMonitorStateException: current thread not owner" at runtime"

  • @manojmehta3226
    @manojmehta3226 4 роки тому

    well explained short and crisp

  • @aniketshah248
    @aniketshah248 3 роки тому

    this passed in Thread() is not runnable obect it is from class Q ,so how it is able to use run ()

  • @leetcodebaby6680
    @leetcodebaby6680 2 роки тому

    Why is valueSet inside while and not inside if?

  • @fazal544
    @fazal544 7 років тому +2

    sir can you please make tutorial videos forconcurreny in java.(like using atomic variabes, use a reentrant ReadWriteLock, use java.util.concurrent collections, synchronizer classes, ExecutorService to concurrently execute tasks, apply fork-join framework)

  • @sumant9120
    @sumant9120 4 роки тому +1

    I had a doubt, are we using the concept of shallow copying here ?

    • @ManishKumar-qx1kh
      @ManishKumar-qx1kh 4 роки тому

      Yes brother. Here as you can see in Producer and Consumer classes we have just created References of Class Q and then we have assigned the location of object passed to them. We are not creating new objects in Consumer and Producer classes instead we are using the object that was passed. Just references we have created. U can say pointers (just to expain i am using c or c++ term).

  • @SajalAgarwal300
    @SajalAgarwal300 5 років тому +4

    All videos of Multi threading are good except this one. Please explain step by step. Cant understand why waiting is going on.

  • @159saur
    @159saur 4 роки тому

    Sir can you please make a video on thread pool and thread group.

  • @bryanliu727
    @bryanliu727 4 роки тому

    Hi Navin, I find that if we don't use the notify() method and 'synchronized', it also works fine. Is it the right way to do it like that?

    • @swaroop6044
      @swaroop6044 4 роки тому +1

      There maybe a case in which both threads will wait on each other and may lead to deadlock , it's better to use wait and notify as a pair.

  • @TOMGAMING-hy9hi
    @TOMGAMING-hy9hi 4 роки тому

    this is a very complex code for beginner.

  • @manishpandey1125
    @manishpandey1125 6 років тому +1

    Why while loop not run infinite....the condition is always true?

  • @meetamin7343
    @meetamin7343 3 роки тому

    AWESOME♥♥

  • @lavanyabheemini3618
    @lavanyabheemini3618 7 років тому +1

    very useful

  • @babymuff2000
    @babymuff2000 2 роки тому

    It gives me an error saying class producer nd consumer is already defined

  • @soumyajitdutta2324
    @soumyajitdutta2324 4 роки тому

    Sir, why you have made the put( ) and get( ) function synchronized

    • @shaikzaheer1181
      @shaikzaheer1181 4 роки тому

      We are using wait method so , it is mandatory to keep the method as synchronized

  • @evankadsouza8058
    @evankadsouza8058 4 роки тому +1

    Sir, while creating the Thread object in Producer class, is this keyword used to call Q's class object ??

  • @AjayDewari
    @AjayDewari 7 років тому +4

    Why u have used while(valueSet), why not if(valueSet)

    • @lazarof94
      @lazarof94 7 років тому

      Google "Java Spurious wake up".

  • @shwetachavan584
    @shwetachavan584 Рік тому

    How to count number of producer and consumer

  • @lpatrasco
    @lpatrasco 6 років тому +6

    Very wrong solution. There is no even inter-thread communication here except that horrible while loop. Go learn the true consumer/ producer pattern in multithreading using wait/notify or simply a blocking queue.

  • @mohsentavakoli3238
    @mohsentavakoli3238 7 років тому +2

    this course Is great, thank you so much. awesome!

  • @technp8780
    @technp8780 5 років тому

    i din't understand Q q; how its possible please someone explain ..

  • @lakhankale9692
    @lakhankale9692 5 років тому

    sir is it necessary to wait in get method if flag is already true
    //
    class Count
    {
    int count;
    boolean flag = false ;
    public synchronized void put(int count)
    {
    if(flag)
    {
    try { wait();}catch (Exception e) {
    }
    }
    System.out.println("put : "+count);
    this.count=count;
    flag=true;
    notify();
    }
    public synchronized void get()
    {
    /*if(!flag)
    {
    try { wait();}catch (Exception e) {
    }
    }*/
    System.out.println("get : "+count);
    flag=false;
    notify();
    }
    }

  • @believer-n3t
    @believer-n3t 3 роки тому

    Thread t = new Thread (this)
    Kya koi bata sakta he this keyword kyon use kiya gya he please

  • @walterp7125
    @walterp7125 7 років тому +1

    you have to implement "synchronized "

  • @ArunraajSingh
    @ArunraajSingh 7 років тому +1

    why inter-thread communication?

    • @gatheringwithin
      @gatheringwithin 7 років тому +2

      to synchronize calculations.. if you are calculating a big set of data and some parts can be done independently while some needs to be all finished before the next step can take place.. this is when one thread needs to know what the other thread is doing.. when you iterate on the same data..

  • @Ahmed-gf9bx
    @Ahmed-gf9bx 3 роки тому

    How many Ads you are subscribed to buddy 🤔
    Wondering, are we marketing in the name of Java

  • @imdadabro4118
    @imdadabro4118 4 роки тому

    can anyone explain the line Thread(this,"Producer");???

    • @ajayarya2738
      @ajayarya2738 4 роки тому

      "Producer" is the name of thread.

  • @aesthetic_sharma
    @aesthetic_sharma 5 років тому

    The guy is so confused. Still helpful :p

  • @destroyerofchalk6456
    @destroyerofchalk6456 5 років тому

    thank you, guy!

  • @ErShikharChauhan
    @ErShikharChauhan 5 років тому +3

    Seems like u know things, but need to practice how to explain things

  • @johnsumith2107
    @johnsumith2107 2 роки тому

    everything is good i didn't understand value set

  • @sambitpradhan5463
    @sambitpradhan5463 6 років тому +1

    Great

  • @ravikirandhulipala4459
    @ravikirandhulipala4459 6 років тому

    thank u sir.... a great explanation .............

  • @jenilgandhi2111
    @jenilgandhi2111 3 роки тому

    java the complete reference ki copy hai yeh line to line code same hai

  • @venpro8705
    @venpro8705 4 роки тому

    Give one more example sir

  • @vivekmalhan7083
    @vivekmalhan7083 5 років тому +5

    Sir it's too complex program to understand for beginners.

    • @ManishKumar-qx1kh
      @ManishKumar-qx1kh 4 роки тому

      start with easy ones brother by urself.I mean u can try simple thread creation and sleep calls in a simple code then try to figure out whats how this is getting callled and whats happening on method call.

  • @preethamm.n1161
    @preethamm.n1161 5 років тому

    💞💞💞💞💞Awesome 🇮🇳💞💞💞💞

  • @hemantdas9546
    @hemantdas9546 5 років тому +4

    Multithreading in Java by your channel is really poor..no clarity at all.

    • @ProgrammerCpp1999
      @ProgrammerCpp1999 5 років тому

      True I recommend you to read Head First Java multithreading is explained nicely in that book

  • @johnvick8861
    @johnvick8861 Рік тому

    Why he used Q q ? That's where I didn't get

    • @automobili2801
      @automobili2801 Рік тому

      so that we can use q methods which is get and set

  • @satishyadav-uo9rn
    @satishyadav-uo9rn Рік тому

    ye chatgpt wale ads block kar do sir, if its possible

  • @shahidzbi
    @shahidzbi 5 років тому

    hhuh what are you trying to tell us???????

  • @saurabhgupta6080
    @saurabhgupta6080 3 роки тому +1

    Worst Explanation. Many things are wrong in this example. Refer Java concurrency in practice. Merely getting the right output is not enough in multithreaded programming.

    • @harshwardhanshetty4683
      @harshwardhanshetty4683 10 місяців тому

      yes i understood that after giving interview in oracle these online videos are somewhat not clear

  • @premjoshuasaldana5149
    @premjoshuasaldana5149 10 місяців тому

    why are u so confused

  • @poojajadhav3732
    @poojajadhav3732 4 роки тому

    Too confusing

  • @Mynksonam
    @Mynksonam 3 роки тому

    the guy is little weak in multi Threading.
    among all video this was less understandable.

  • @bisht173
    @bisht173 Рік тому

    Didn’t like this video. More I try to understand using this video, more confused I get. Please try to explain the use case first and also do you homework before creating the video so that you don’t have to do so many hit and try in the video.👎

  • @UCTNX4PiSl496AhgUyGlB5wg
    @UCTNX4PiSl496AhgUyGlB5wg 5 років тому

    Disappointed

  • @hitesh-og3nh
    @hitesh-og3nh 19 днів тому

    dhang se to padhao sir
    kuch smj ni aaya

  • @safiullahrahmani2807
    @safiullahrahmani2807 3 роки тому

    Just copied the whole thing from herbert manuel book!!! That's why i hate watchting learning videos on youtube. You learn nothings new. :/