Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we get NPE, race condition

I was asked the following question on an interview. Given the following code, if methods add and doAction are being invoked by multiple threads, how can we get a NullPointerException when printing toString?**

public class Test{
     private List<Object> obj = new ArrayList<Object>();

     public void add(Object o){
           obj.add(o);
     }

     public void doAction(){
           for(Object o: obj){
                 System.out.println(o.toString()); // maybe NPE, why?
           }
     }

}

Cut out all other multithread concerns.

like image 398
St.Antario Avatar asked Apr 04 '17 16:04

St.Antario


2 Answers

First, let's change the name of the variable; List<Object> list=new ArrayList<>(); because "obj" is a really awful name for a variable that refers to a List.

OK, When the program calls list.add(o);, it may need to grow the array. That means it's got to:

  1. Allocate a new, bigger array whose members all will be initialized to null, and
  2. Copy the elements from the old array to the new array.,

If thread A is doing that while at the same time thread B is calling iterator.next(), thread B could end up reading a null value from the new array even after thread A has already copied an object reference into that member of the array.

Remember: When threads access memory with no synchronization, then it is possible for a reader thread to see updates to variables/fields/array members happen in a different order from the program order in which the writing thread actually performed them.

like image 181
Solomon Slow Avatar answered Oct 26 '22 21:10

Solomon Slow


Test t = new Test();
t.add(null);
t.doAction(); //NPE triggered

No nullability guarentees on the obj list, so it might contain null values.

Regarding the problem with multithreading refers to the ConcurrentModificationException since the "for-all" look uses an Iterator internally. If an element is added while iterating it will cause an exception to be thrown.

like image 31
Kiskae Avatar answered Oct 26 '22 21:10

Kiskae