title: How use long instead of int can bulletproof the following method - Effective Java
body: <p>Consider the following code picked from Joshua Bloch - Effective Java, page 263</p>
  
  <pre><code>// Broken - requires synchronization!
  private static volatile int nextSerialNumber = 0;
  public static int generateSerialNumber() {
      return nextSerialNumber++;
  }
  </code></pre>
  
  <blockquote>
    <p>One way to fix the
    generateSerialNumber method is to add
    the synchronized modifier to its
    declaration. This ensures that
    multiple invocations won’t be
    interleaved, and that each invocation
    will see the effects of all previous
    invocations. Once you’ve done that,
    you can and should remove the volatile
    modifier from nextSerialNumber. To
    bulletproof the method, use long
    instead of int, or throw an exception
    if nextSerialNumber is about to wrap.</p>
  </blockquote>
  
  <ol>
  <li>I understand that we can remove <code>volatile</code> after we make generateSerialNumber <code>synchronized</code>, as it is redundant. But, does it make any harm? <strong>Any performance penalty if I am having both synchronized and volatile like</strong></li>
  </ol>
  
  <hr>
  
  <pre><code>private static volatile int nextSerialNumber = 0;
  public static synchronized int generateSerialNumber() {
      return nextSerialNumber++;
  }
  </code></pre>
  
  <hr>
  
  <ol>
  <li>What does, <em>use long instead of int</em> means? <strong>I do not understand how this bulletproof the method?</strong></li>
  </ol>