title: Java Synchronized method is not locking object
body: <p>I wrote a java program to generate unique id for each row in a file. There could be multiple jobs running same program to get unique ids. This id has to be unique for each row in any file. I tried to achieve this by serializing value to a file. Since multiple jobs will be using this program I made the method synchronized. Code works fine for single job but when tried to run multiple jobs at same time synchronized method fails. Looks like method is called again on same object when there is one already running. Why is synchronized method not locking the object?</p>
  
  <p>Below is the id generate code I wrote –</p>
  
  <pre><code>public class CreateId2 implements Serializable {
  
  private static final long serialVersionUID = 1L;
  private long i;
  
  public synchronized long idGenerate() {
  
      long k = this.generateId(this);
      return k;
  
  }
  private long generateId(CreateId2 id){
  
      long returnedVal = id.getI();
      try {
      returnedVal = id.readObject();
      //System.out.println("previous modiefiied returnedVal is" + returnedVal);
      } catch (FileNotFoundException  ex) {
          id.setI(200L); //start from 200 for the first time
          returnedVal=id.getI();
      } catch (Exception ex2) {
          ex2.printStackTrace();
      }
      returnedVal = returnedVal +1;
      id.setI(returnedVal);
      //System.out.println("returnedVal is " + returnedVal);
      try {
      id.saveObject(id);
      } catch (Exception ex3) {
          ex3.printStackTrace();
  
          }
      return returnedVal;
  }
  
  public void saveObject(CreateId2 id) throws IOException{
      //System.out.println("saving object " + id.getI());
      File savedFileName = new File("C:\\Users\\sam\\Desktop\\tests\\test.ser");
      FileOutputStream savedOutput = new FileOutputStream(savedFileName);
      ObjectOutputStream oos = new ObjectOutputStream(savedOutput);
  
      oos.writeObject(id);
      oos.flush();
      // close the writing.
      oos.close();
      //System.out.println("saved. value is " + id.getI());
  }
  
  public long readObject() throws IOException, ClassNotFoundException{
      //System.out.println("read object is called. value is " + ci2.getI());
  
      File savedFileName = new File("C:\\Users\\sam\\Desktop\\tests\\test.ser");
      FileInputStream savedInput = new FileInputStream(savedFileName);
      ObjectInputStream ois = new ObjectInputStream(savedInput);
  
      //long val = Long.parseLong(ois.readObject().toString());
  
      CreateId2 ci2 = (CreateId2) ois.readObject();
      //System.out.println("value of valtemp " + ci2.getI());
      long val = Long.valueOf(ci2.getI());
  
      ois.close();
      return val;
      // close the writing.
  
  }
  
  public  long getI() {
      return i;
  }
  
  public void setI(long i) {
      this.i = i;
      //CreateId2.i = i;
  }
  
  // setter methods
  
  private static CreateId2 instance = null;
  private CreateId2() { }
  public static CreateId2 getInstance() {
     if (instance == null) {
         instance = new CreateId2();
     }
     return instance;
  }
  
  }
  </code></pre>
  
  <p>I m calling this method like this -</p>
  
  <pre><code>CreateId2 id = CreateId2.getInstance();
  long j = id.idGenerate();
  </code></pre>
  
  <p>When I call this method simultaneously I get this below error. Also generated ids are not unique anymore.</p>
  
  <pre><code>java.io.EOFException
      at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325)
      at java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(ObjectInputStream.java:2806)
      at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2864)
      at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1072)
      at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:704)
      at java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:830)
      at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1601)
      at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
      at helperPackage.CreateId2.readObject(CreateId2.java:66)
      at helperPackage.CreateId2.generateId(CreateId2.java:23)
      at textFile.RandomTest.main(RandomTest.java:18)
  </code></pre>