title: Are arrays thread-safe in Java?
body: <p>Are there any concurrency problems with one thread reading from one index of an array, while another thread writes to another index of the array, as long as the indices are different?</p>
  
  <p>e.g. (this example not necessarily recommended for real use, only to illustrate my point)</p>
  
  <pre><code> class Test1
   {
       static final private int N = 4096;
       final private int[] x = new int[N];
       final private AtomicInteger nwritten = new AtomicInteger(0);
       // invariant: 
       // all values x[i] where 0 &lt;= i &lt; nwritten.get() are immutable
  
       // read() is not synchronized since we want it to be fast
       int read(int index) {
           if (index &gt;= nwritten.get())
               throw new IllegalArgumentException();
           return x[index];
       }
       // write() is synchronized to handle multiple writers
       // (using compare-and-set techniques to avoid blocking algorithms
       // is nontrivial)
       synchronized void write(int x_i) {
           int index = nwriting.get();
           if (index &gt;= N)
               throw SomeExceptionThatIndicatesArrayIsFull();
           x[index] = x_i;
           // from this point forward, x[index] is fixed in stone
           nwriting.set(index+1);
       }     
   }
  </code></pre>
  
  <p><strong>edit:</strong> critiquing this example is not my question, I literally just want to know if array access to one index, concurrently to access of another index, poses concurrency problems, couldn't think of a simple example.</p>