java – How do I provoke race conditions on this non-threadsafe arraylist class?


I’m playing around with trying to build a arraylist class that is threadsafe

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<1000; i++) {
executorService.execute(new Runnable() {
  public void run() {
    for (int i=0; i<10; i++)
      dal1.add(i);
  }
 });}
System.out.println("Using toString(): " + dal1);


for (int i=0; i<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();
}

public static LongArrayList withElements(long... initialValues){
   LongArrayList list = new LongArrayList();
   for (long l : initialValues) list.add( l );
   return list;
}

// reset me to initial 
public void reset(){
  items = new long(2);
  size = 0;
}

// Number of items in the double list
public int size() {
  return size;
}

// Return item number i
public long get(int i) {
  if (0 <= i && i < size) 
   return items(i);
  else 
    throw new IndexOutOfBoundsException(String.valueOf(i));
   }

   // Replace item number i, if any, with x
   public long set(int i, long x) {
   if (0 <= i && i < size) {
    long old = items(i);
    items(i) = x;
    return old;
   } else 
     throw new IndexOutOfBoundsException(String.valueOf(i));}

   // Add item x to end of list
   public LongArrayList add(long x) {
   if (size == items.length) {
   long() newItems = new long(items.length * 2);
   for (int i=0; i<items.length; i++)
       newItems(i) = items(i);
  items = newItems;
}
items(size) = x;
size++;
return this;
}

public String toString() {
  return Arrays.stream(items, 0,size)
  .mapToObj( Long::toString )
  .collect(Collectors.joining(", ", "(", ")"));
}
}

The “Longaraylist” class is simply a class for a list that is not threadsafe, this is the class that I later want to manipulate to become threadsafe.
What is bugging me right now is the driver code in main, I am creating a executorservice, and I am submitting a bunch of tasks to it. In my mind, using this non-threadsafe class, this should introduce some race conditions, and the “size” variable should at the very least be broken. But every time I run this code, this runs perfectly. The size that I print at the end perfectly corresponds to the amount of times that add() is called.

What could I do differently? Maybe I am using the executorservice wrong?
I would also be grateful of any other tips to test the threadsafety of such a class. thank you