title: multiple threads accessing an ArrayList
body: <p>i have an ArrayList that's used to buffer data so that other threads can read them</p>
  
  <p>this array constantly has data added to it since it's reading from a udp source, and the other threads constantly reading from that array.Then the data is removed from the array.</p>
  
  <p>this is not the actual code but a simplified example :</p>
  
  <pre><code> public class PacketReader implements Runnable{
  
     pubic static ArrayList&lt;Packet&gt; buffer = new ArrayList() ;
      @Override 
      public void run(){
      while(bActive){
      //read from udp source and add data to the array
    }
   }
  
   public class Player implements Runnable(){
  
   @Override 
   public void run(){
  
  
   //read packet from buffer
   //decode packets
  
   // now for the problem :
  
   PacketReader.buffer.remove(the packet that's been read);
       }
     }
  </code></pre>
  
  <p>The remove() method removes packets from the array and then shifts all the packets on the right to the left to cover the void.</p>
  
  <p>My concern is : since the buffer is constantly being added to and read from by multiple threads , would the remove() method make issues since its gonna have to shift packets to the left? </p>
  
  <p>i mean if .add() or .get() methods get called on that arraylist at the same time that shift is being done would it be a problem ?</p>
  
  <p>i do get index out of bounds exception sometimes and its something like :
  index : 100  size 300 , which is strange cuz index is within size , so i want to know if this is what may possibly be causing the problem or should i look for other problems .</p>
  
  <p>thank you</p>