Does notify/notifyall release the lock being held

前端 未结 8 1431
忘掉有多难
忘掉有多难 2020-12-09 02:41

I am confused a bit about wait and notify/notifyAll.

I know there is a lock for every java object. I know wait will release the lock for other thread. How about noti

相关标签:
8条回答
  • 2020-12-09 03:11
    public class ProducerConsumerInJava {
        public static void main(String args[]) {
          System.out.println("How to use wait and notify method in Java"); 
          System.out.println("Solving Producer Consumper Problem"); 
          Queue<Integer> buffer = new LinkedList<>(); 
          int maxSize = 10; 
          Thread producer = new Producer(buffer, maxSize, "PRODUCER"); 
          Thread consumer = new Consumer(buffer, maxSize, "CONSUMER"); 
          producer.start(); 
          consumer.start(); 
        }
    }
    
    class Producer extends Thread {
       private Queue<Integer> queue; 
       private int maxSize; 
       public Producer(Queue<Integer> queue, int maxSize, String name){ 
                super(name); this.queue = queue; this.maxSize = maxSize; 
       }
    
       public void run() { 
         while (true) { 
                  synchronized (queue) { 
                        while (queue.size() == maxSize) { 
                              try { 
                                    System.out .println("Queue is full, " +                         
                             "Producer thread waiting for " + "consumer to take 
                              something from queue"); 
                              queue.wait(); 
                        } catch (Exception ex) { 
                            ex.printStackTrace(); 
                            } 
                    }
          Random random = new Random(); 
          int i = random.nextInt(); 
          System.out.println("Producing value : " + i); 
          queue.add(i); 
          queue.notifyAll(); 
         } 
        } 
      } 
      }
    
     class Consumer extends Thread {
       private Queue<Integer> queue; 
       private int maxSize; 
       public Consumer(Queue<Integer> queue, int maxSize, String name){ 
                super(name); this.queue = queue; this.maxSize = maxSize; 
       }
    
       public void run() { 
         while (true) { 
                  synchronized (queue) { 
                        while (queue.isEmpty()) { 
                              try { 
                                    System.out .println("Queue is empty," +                         
                             "Consumer thread is waiting" +
                              " for producer thread to put something in queue");
                              queue.wait(); 
                        } catch (Exception ex) { 
                            ex.printStackTrace();
                          }
                      } 
           System.out.println("Consuming value : " + queue.remove()); 
           queue.notifyAll(); 
          } 
        } 
      } 
      }
    

    This is an example of Consumer and Producer program.

    Output of the above program after execution is written below:

    How to use wait and notify 
    method in Java Solving Producer Consumper Problem 
    Queue is empty,Consumer thread is waiting for producer thread to put 
    something in queue 
    
    Producing value : -1692411980 
    Producing value : 285310787 
    Producing value : -1045894970 
    Producing value : 2140997307 
    Producing value : 1379699468 
    Producing value : 912077154 
    Producing value : -1635438928 
    Producing value : -500696499 
    Producing value : -1985700664 
    Producing value : 961945684 
    Queue is full, Producer thread waiting for consumer to take something from 
    queue Consuming value : -1692411980 
    Consuming value : 285310787 
    Consuming value : -1045894970 
     Consuming value : 2140997307 
    Consuming value : 1379699468 
    Consuming value : 912077154 
    Consuming value : -1635438928 
    Consuming value : -500696499 
    Consuming value : -1985700664 
    Consuming value : 961945684 
    Queue is empty,Consumer thread is waiting for producer thread to put 
    something  in queue 
    
    Producing value : 118213849
    

    So, what we can conclude is, notifyAll() or notify() will not release the lock. have a look at the output, Producing value and Consuming value are not printed alternatively i.e there are printed separately.

    Hence, notify/ notifyAll will not release lock

    Read more: http://javarevisited.blogspot.com/2015/07/how-to-use-wait-notify-and-notifyall-in.html#ixzz57kdToLX6

    0 讨论(0)
  • 2020-12-09 03:11

    Calling notify() method on an object changes the state of the waiting threads. The notifying thread only releases the lock once it completes the execution of its synchronized code on the lock object it is going to release.

    So here's how it is:

    wait() If a thread calls wait() method on an object, the thread IMMEDIATELY releases the lock of that object and goes into waiting state.

    notify() But when a thread calls notify() method on an object, the thread does not release the lock of that object immediately, if the thread have some more job to do (i.e code to execute after notify() call). If the execution of the synchronized code is completed or there are no statements after notify(), then the thread releases the lock for waken up threads from waiting state.

    0 讨论(0)
提交回复
热议问题