title: Synchronizing static vector
body: <p>I have few doubts regarding the Vector in the code below being static.
  1. by removing the static example</p>
  
  <pre><code>public class WaitAndNotify extends Thread   {
  
  private String info;
  Vector aVector;
  .
  .
  //same code as below over here
  .
  .
  public static void main (String args []) {
  Vector aVector =new Vector();
      new WaitAndNotify("first", aVector).start();
      new WaitAndNotify("second", aVector).start();
      new WaitAndNotify("last", aVector).start();
  }
  </code></pre>
  
  <p>the output still remains the same(synchronized). why? if Vector is non-static then each thread,will have its own copy of Vector. So the output should not be syncronized. But it is. why?</p>
  
  <p>2.if the code if as follows:</p>
  
  <pre><code>public class WaitAndNotify extends Thread   {
  
  private String info;
  static  Vector aVector = new Vector();
   public WaitAndNotify (String info) {
      this.info = info;
  }
  .
  .
  //code
  .
  .
  public static void main (String args []) {
      new WaitAndNotify("first").start();
      new WaitAndNotify("second").start();
      new WaitAndNotify("last").start();
  }
  </code></pre>
  
  <p>And now here if i Remove static and make vector non-static then the output will not be synchronized. Why so?</p>
  
  <p>Actual code:</p>
  
  <pre><code>import java.util.Vector;
  
  public class WaitAndNotify extends Thread   {
  
  private String info;
  static  Vector aVector = new Vector();
  
  public WaitAndNotify (String info, Vector aVector) {
      this.info = info;
      this.aVector = aVector;
  }
  
  public void doTheJob() {
     synchronized ( aVector ) {
      if (  info.equals("last") ) {
          System.out.println(info + " is waking up ...");
          aVector.notifyAll();
          System.out.println(info + " done.");
      } else {
          System.out.println(info + " is waiting");
          try {
              aVector.wait();
          } catch ( IllegalMonitorStateException  e ) {
              System.out.println(info +
                ": IllegalMonitorStateException");
          } catch ( InterruptedException  e ) {
              System.out.println(info +
                ": InterruptedException");
          }
          System.out.println(info + " is awake!");
      }
    }
  }
  
  
  public void run () {
      doTheJob();
  }
  
  public static void main (String args []) {
      new WaitAndNotify("first", aVector).start();
      new WaitAndNotify("second", aVector).start();
      new WaitAndNotify("last", aVector).start();
  }
  }
  </code></pre>