<Java><Multi-thread>

Source: Internet
Author: User

Overview
    • Notify () VS Notifyall ()
From a basic example
  • One of the most classic producer consumer models:
  • public synchronized void put (String value) throws interruptedexception{WH            Ile (buffer.size () = = capacity) {System.out.println (Thread.CurrentThread (). GetName () + "is waiting ...");        Wait ();        } buffer.add (value);        System.out.println (Thread.CurrentThread (). GetName () + "has produced" + value);    Notify (); Synchronized String get () throws interruptedexception{while (buffer.size () = = 0) {SYSTEM.O            Ut.println (Thread.CurrentThread (). GetName () + "is waiting ...");        Wait ();        } String val = buffer.remove (0);        Notify ();        System.out.println (Thread.CurrentThread (). GetName () + "has consumed" + val);    return Val; }
  • First, the first question is, why is the get and put internally used while loops ? Answer: We need a while loop in case we get this situation:
    • C1 into the critical section, found that size is empty, so wait;
    • P1 enters the critical zone, adding buffer, and notify;
    • At the same time, C2 is also waiting to enter the critical zone (block blocking state);
    • C1 was notified, but C1 not preemption to lock, because C2 entered the critical area, consumption of buffer, then buffer is empty;
    • After that, C1 has a lock, but at this point the buffer is empty, so there must be a while loop to judge again.
  • Then, why do I need Notifyall ()?
    • We all know that notify () is informing a thread, while Notifyall () is informing all threads. What's the difference, specifically?
    • Consider a queue with buffer 1 (for simplicity):
    • STEP 1:
      -P1 puts 1 char into the buffer

      STEP 2:
      -P2 attempts put -checks wait Loop-already a char-waits "P2 in Wait"

      STEP 3:
      -P3 attempts put -checks wait Loop-already a char-waits "P2, P3 in Wait"

      STEP 4:
      -C1 attempts to get 1 char
      -C2 attempts to get 1 char-blocks in entry to get the method "C2 block"
      -C3 attempts to get 1 char-blocks in entry to get the method "C2, C3 block"

      STEP 5:
      -C1 is executing the get method-gets the char, calls notify , exits method
      -The notify wakes up P2
      -but, C2 enters method before P2 can (P2 must reacquire the lock), so P2 blocks on entry to the put method "P3 Wait" "P2, C3 Blocking
      -C2 checks wait loop, no more chars in buffer, so Waits "P3, C2 Waiting" "P2, C3 block"
      -C3 enters method after C2, but before P2, checks wait loop, no more chars in buffer, so Waits "C3, C2, P3 Wait" "P2 block"

      STEP 6:
      -Now:there is P3, C2, and C3 waiting!
      -finally P2 acquires the lock, puts a char in the buffer, calls notify, exits method "P2 finally grabbed the lock"

      STEP 7:
      -P2 ' s notification wakes P3 (remember any thread can be woken) "P3 was notified"
      -P3 checks the wait loop condition, there is already a char in the buffer, so waits. "P3, C2, C3 Wait" "Block queue is empty"
      -NO more THREADS to call NOTIFY and three THREADS permanently suspended!

    • The solution to this deadlock problem is to Replace all notify () with Notifyall (). This makes it possible to notifyall all waiting threads into a blocking state every time.
  • Above, is actually caused the difference between the two states of blocked and wait.
FYI
    • StackOverflow

<Java><Multi-thread>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.