title: how to access List of lists concurrently
body: <p>I have a List of Lists , what i want to do is create threads which reads objects(list) and removes it from the "sharedList" , how can make the list threadsafe ? am using synchronizedList , but what if a thread removed and object while another thread was reading another object ? as .remove shifts the objects to the left ....</p>
  
  <p>filler class</p>
  
  <pre><code>public class DataFetcher implements Runnable {
  
      Session session = null;
      Criteria criteria = null;
      private List&lt;Parts&gt; partList = null;
  
  
  
  
      public DataFetcher() {
  
      }
  
      @Override
      public void run() {
  
          // while(true)
          List newPartList =getNParts(DBIndexGuard.getAllowedListSize());
          SharedList.addPartList(newPartList);
  
  
      }
  
      public List&lt;Parts&gt; getNParts(int listSize) {
  
          try {
              session = HibernateUtils.getSessionFactory().openSession();
              criteria = ....
              // *********** SYNCHRONIZATION OCCURS HERE ********************//
              criteria.setFirstResult(DBIndexGuard.getNextIndex());
              criteria.setMaxResults(listSize);
              partList = criteria.list();
  
  
  
          } catch (Exception e) {
              e.printStackTrace();
  
          } finally {
  
              session.close();
          }
          return partList;
      }
  
  }
  </code></pre>
  
  <p>sharedList class</p>
  
  <pre><code>public class SharedList {
  
      private static final List&lt;List&lt;Parts&gt;&gt; sharedList = Collections.synchronizedList(new ArrayList());
  
      public List&lt;List&lt;Parts&gt;&gt; SharedList() {
  
          return sharedList;
  
      }
  
      static synchronized void addPartList(List partList) {
          sharedList.add(partList);
      }
  
      static synchronized List getPartList(int index) {
  
          return sharedList.get(index);
      }
  
      static synchronized void removePartList(int index) {
          sharedList.remove(index);
      }
  }
  </code></pre>
  
  <p>the worker thread</p>
  
  <pre><code>public class Finder implements Runnable {
      private List&lt;Parts&gt; partList = null;
  
      public Finder () {
      }
  
      @Override
      public void run() {
  
          operateOnList(partList);
      }
  
      public void operateOnList(List&lt;Parts&gt; partList) {
          for (Parts part : partList) {
  
          }
  
      }
  }
  </code></pre>
  
  <p>I will be creating multi finders to work on the sharedList</p>