-3

I am trying to understand how ArrayList is not thread safe through a java program.Attached is my program.

import java.util.ArrayList;
import java.util.List;
public  class class1 
 {
  static List ar=new ArrayList(1);
  public static void main(String[] args) throws InstantiationException,   
  IllegalAccessException, ClassNotFoundException, InterruptedException 
   {
      Thread t1= new Thread()
      {
          public void run()
          {
              while(true)
              {
                 ar.add(new Object());
              }
          }
      };

      Thread t2=new Thread()
      {
              public void run()
          {
              while(true)
              {
                  ar=new ArrayList(1);
                  ar.add(new Object());
                  ar.add(new Object());
              }
          }
      };

      t1.start();
      Thread.sleep(100);
      t2.start();
      }
    }

The error i got is:

  Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 2
  at java.util.ArrayList.add(Unknown Source)
  at class1$1.run(class1.java:22)

I understand that the exception is caused by a thread.However,I am not getting a broader picture on how it is actually functioning.Any help would be highly appreciated.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
mohan babu
  • 1,388
  • 2
  • 17
  • 35
  • 3
    This exception is not cause by a thread, its caused by your array going out of bounds as clearly stated by `java.lang.ArrayIndexOutOfBoundsException: 2` – user1231232141214124 Jan 08 '16 at 19:42
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Atri Jan 08 '16 at 19:43
  • How did you come up with this program and how exactly do you think it demonstrates threadsafety vs. non-threadsafety? – Kayaman Jan 08 '16 at 19:47
  • @redFIVE, technically everything that happens in Java is "caused by a thread." I think what you mean, is that the reason for the exception has nothing to do with the fact that the ArrayList being _shared by more than one thread_. – Solomon Slow Jan 08 '16 at 19:47
  • @jameslarge Except, of course, **this has everything to do with it**. The only function used on the `ArrayList` is `add`, which should not, under normal circumstances (where "normal" means single-threaded) throw a out-of-bounds. – Ordous Jan 08 '16 at 19:55
  • @Ordous, True enough. – Solomon Slow Jan 08 '16 at 20:04

1 Answers1

1

Look at ArrayList.Add code. Array list is based on arrays. If array is full - arrayList expands it's length on 2. To expand an array it has to be copied. Looks like your code breaks down when "add" called in one thread expands the arrya and trying to copy data - but another thread changes the reference.

Azee
  • 1,809
  • 17
  • 23
  • 1
    Close, but no cigar (no downvote though, as everything but the guess and factor is correct). Interleaving `add` and a change of reference will do no harm, because the `add` will be done completely on the old object. It's to do with 2 `add` methods interleaving. It's actually non-trivial to find how they do it, I would encourage you to try and spot it (hint - it's in the `grow` method). – Ordous Jan 08 '16 at 20:06