title: Why isn't synchronization taking place in this code?
body: <pre><code>package atask;
  
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  
  
  public class Q3Synchronization 
  {
      Integer sum=new Integer(0);
  
       Q3Synchronization(){
           ExecutorService e=Executors.newFixedThreadPool(1000);
           for(int i=0;i&lt;1000;i++){
               e.execute(new Sum());
           }
           e.shutdown();
  
           while(!e.isTerminated()){
  
           }
           System.out.println(sum);
       }
      public static void main(String[]args){
          new Q3Synchronization();
      }
  
       class Sum implements Runnable{
  
  
          @Override
          public void run() {
  
              m();
  
          }
         public synchronized void m(){
               sum=sum+1;
           }
       }
  
  }
  </code></pre>
  
  <p>The question is:</p>
  
  <ul>
  <li>(Synchronize threads) Write a program that launches 1,000 threads. Each thread adds 1 to a variable sum that initially is 0. You need to pass sum by reference to each thread. In order to pass it by reference, define an Integer wrapper object to hold sum. Run the program with and without synchronization to see its effect.</li>
  </ul>