title: Is this concurrent code slower because of overhead? or is something else at play?
body: <p>I'm playing around with trying to build a arraylist class that is made threadsafe in a very clumsy way by just slapping on the synchronized keyword on all methods</p>
  <pre><code>import java.util.stream.*;
  import java.util.Arrays;
  import java.util.Random;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  public class LongArrayListUnsafe {
  public static void main(String[] args) {
  LongArrayList dal1 = LongArrayList.withElements();
  
  ExecutorService executorService =  Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  
  for (int i=0; i&lt;1000; i++) {
  executorService.execute(new Runnable() {
    public void run() {
      for (int i=0; i&lt;10; i++)
        dal1.add(i);
    }
   });}
  System.out.println(&quot;Using toString(): &quot; + dal1);
  
  
  for (int i=0; i&lt;dal1.size(); i++)
    System.out.println(dal1.get(i));
  
  System.out.println(dal1.size());} }
  
  class LongArrayList {
  
  private long[] items;
  private int size;
  
  public LongArrayList() {
     reset();
  }
  
  synchronized public static LongArrayList withElements(long... initialValues){
     LongArrayList list = new LongArrayList();
     for (long l : initialValues) list.add( l );
     return list;
  }
  
  // reset me to initial 
  synchronized public void reset(){
    items = new long[2];
    size = 0;
  }
  
  // Number of items in the double list
  synchronized public int size() {
    return size;
  }
  
  // Return item number i
  synchronized public long get(int i) {
    if (0 &lt;= i &amp;&amp; i &lt; size) 
     return items[i];
    else 
      throw new IndexOutOfBoundsException(String.valueOf(i));
     }
  
     // Replace item number i, if any, with x
     synchronized public long set(int i, long x) {
     if (0 &lt;= i &amp;&amp; i &lt; size) {
      long old = items[i];
      items[i] = x;
      return old;
     } else 
       throw new IndexOutOfBoundsException(String.valueOf(i));}
  
     // Add item x to end of list
     synchronized public LongArrayList add(long x) {
     if (size == items.length) {
     long[] newItems = new long[items.length * 2];
     for (int i=0; i&lt;items.length; i++)
         newItems[i] = items[i];
    items = newItems;
  }
  items[size] = x;
  size++;
  return this;
  }
  
  synchronized public String toString() {
    return Arrays.stream(items, 0,size)
    .mapToObj( Long::toString )
    .collect(Collectors.joining(&quot;, &quot;, &quot;[&quot;, &quot;]&quot;));
  }
  }
  </code></pre>
  <p>The relevant thing I'm doing is adding a bunch elements to a list, with some tasks.  The issue is that when I increase the amount of threads that I pass to the fixedthreadPool, my code runs in the same time as when I only pass only one thread, maybe even slower.
  I have three theories on why this is:</p>
  <ul>
  <li>This is because of thread overhead, and the tasks I am creating are simply too small, I need to make them bigger before it pays off to use more threads.</li>
  <li>It has to do with lock contention, because my class is so clumsily threadsafe, the threads a are competing for the locks, and somehow slowing down everything</li>
  <li>I'm making a completely obvious mistake in using the threadexecutorpool</li>
  </ul>