title: Is this the correct way of using synchronized in Java?
body: <p>The for loop shown here is run within a thread. Inside the sychronized block, a thread writes to some file. There are several different files, so the writers are kept in an array. What I want to make sure here is that no two different threads are writing to the same file at the same time. However, they can write to different files. Am I using the correct parameter with the synchronized block?       </p>
  
  <pre><code>for(Element e: elements)
  {
      int i = getWriterIndex(e)
      writeri = writers(i)
      synchronized(writeri)
      {
          // Write to corresponding segment
          writers(i).write(e)
          recordsWritten(i) += 1
      }
  }
  </code></pre>