title: Understanding synchronized method in JAVA
body: <p>I am new to JAVA and learning multithreading in JAVA. This is my code snippet.</p>
  
  <pre><code>import java.util.*;
  import java.lang.*;
  import java.io.*;
  
  class Manager {
      static final int MAXQUEUE = 5;
      private Vector messages = new Vector();
  
      public synchronized void putMessage() throws InterruptedException {
          while (messages.size() == MAXQUEUE) {
              System.out.println("waiting for space in queue ");
              wait();
          }
          messages.addElement(new java.util.Date().toString());
          System.out.println("created a new message and message count is " + messages.size());
          notify();
      }
  
      public synchronized String getMessage() throws InterruptedException {
          notify();
          while (messages.size() == 0) {
              System.out.println("queue is empty ");
              wait();
          }
          String message = (String) messages.firstElement();
          messages.removeElement(message);
          System.out.println("removed a message and message count is " + messages.size());
          return message;
      }
  }
  
  class Producer extends Thread {
      Manager myRef;
  
      Producer(Manager ref) {
          myRef = ref;
      }
  
      public void run() {
          try {
              while (true) {
                  myRef.putMessage();
                  sleep(1000);
              }
          } catch (InterruptedException e) {
          }
      }
  }
  
  class Consumer extends Thread {
      Manager myRef;
  
      Consumer(Manager ref) {
          myRef = ref;
      }
  
      public void run() {
          try {
              while (true) {
                  String message = myRef.getMessage();
                  System.out.println("Got message: " + message);
                  sleep(2000);
              }
          } catch (InterruptedException e) {
          }
      }
  
      public static void main(String args[]) {
          Manager ref = new Manager();
          Producer producer = new Producer(ref);
          producer.start();
          new Consumer(ref).start();
      }
  }
  </code></pre>
  
  <p><strong>What i am expecting</strong>:</p>
  
  <ol>
  <li>First my Producer thread will take control of the lock on Manager object. It will call putMessage() until the count is 5 and will release the lock.</li>
  <li>Now Consumer thread will take the lock and start reading the message until the list is empty and then it will release the lock.
  This sequence will continue.</li>
  </ol>
  
  <p><strong>But, what is happening</strong>:</p>
  
  <pre><code>created a new message and message count is 1
  removed a message and message count is 0
  Got message: Thu Aug 13 07:26:45 GMT 2015
  created a new message and message count is 1
  removed a message and message count is 0
  Got message: Thu Aug 13 07:26:46 GMT 2015
  and so on.....
  </code></pre>
  
  <p>As you can see, my consumer thread is able to call readMessage() even though the lock of the manager object is with Producer thread which is executing putMessage(). If one thread is executing an instance method, how other thread is able to call the other instance method?</p>
  
  <p>Please correct my understanding.</p>