title: Multi-threads writing same thing to the same file?
body: <p>I alway thought concurrently threads writing to one same file needs synchronization. </p>
  
  <p>What happend when multi-threads writing same thing to the same file without synchronization? I imagined the output file must be incomplete or corrupted. </p>
  
  <pre><code>public class Test 
  {   
    public Runnable createLayoutRunnable() {
      return new Runnable() {
          public void run() {
              try {
                  FileInputStream inputStream = new FileInputStream("mov.mp4");
  
                  FileOutputStream outputStream = new FileOutputStream("mov_co.mp4");
                  //IOUtils.copy(inputStream, outputStream);
  
                  //synchronized ("lock"){
                  int read = 0;
                  byte[] bytes = new byte[1024];
  
                  while ((read = inputStream.read(bytes)) != -1) {
                      outputStream.write(bytes, 0, read);
                  }
                  //}
  
                  System.out.println(Thread.currentThread().getName() + " is done"); 
  
  
  
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      };
  }
  
  
  public static void main(String[] args) {
      Test test = new Test();
      //Create Thread Pool for parallel layout
      ExecutorService executor = Executors.newFixedThreadPool(9);
  
      //Run Tasks and wait for termination in the current thread
      Future&lt;?&gt; f1 = executor.submit(test.createLayoutRunnable());
      Future&lt;?&gt; f2 = executor.submit(test.createLayoutRunnable());
      Future&lt;?&gt; f3 = executor.submit(test.createLayoutRunnable());
      Future&lt;?&gt; f4 = executor.submit(test.createLayoutRunnable());
      Future&lt;?&gt; f5 = executor.submit(test.createLayoutRunnable());
      Future&lt;?&gt; f6 = executor.submit(test.createLayoutRunnable());
      Future&lt;?&gt; f7 = executor.submit(test.createLayoutRunnable());
      Future&lt;?&gt; f8 = executor.submit(test.createLayoutRunnable());
      Future&lt;?&gt; f9 = executor.submit(test.createLayoutRunnable());
  
  
      try {
          f1.get();
          f2.get();
          f3.get();
          f4.get();
          f5.get();
          f6.get();
          f7.get();
          f8.get();
          f9.get();
  
  
      } catch (Exception ex) {
          ex.printStackTrace();
      }
      executor.shutdown();
  
      System.out.println("all done");
  
  }
  
  }
  </code></pre>
  
  <p>Surprise! The output mov is good to play! 
  How come? Please help! </p>
  
  <p>Edit: Before all, I'm terribly sorry about the confusion. Yes the first-time code I posted was synchronised as opposed to what I said. I have commented it off now. This is because I was playing with the code and that's where I found whether it's synchronised or not doesn't matter and was wondering why. </p>