You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Kumar, Vasanth" <Va...@InterWorld.com> on 2001/11/26 21:07:08 UTC

Question about FastArrayList

To the designer(s)/author(s) of collections class FastArrayList:

I was looking through the collections project and in particular I am
interested in FastArrayList.  However, I am concerned about possible isses
with it.

1. When fast is true, assume that Thread 1 is inside get(int index) method
on the line: return (list.get(index));
Meanwhile, Thread 2 sneaks in and calls setFast(false) changing fast to be
false AND calls list.remove(int index).  The list.remove() locks the list,
but Thread 1 is actually accessing the list object at the same time without
locking it.

Suggestion:
Changing the fast flag on a FastArrayList from false to true should be ok,
but changing it from true to false (i.e. from fast to slow) should be
prevented.  

This is because when switching fast mode to slow mode, already executing
methods in fast mode need to complete the calls before the mode can be
changed to slow.  And the way to ensure this would be be to do more
synchronization (including in the get() method) - which would defeat the
purpose of the FastArrayList class (to allow for unsynchronized access).

2. The code in add(int index, Object element) could be a problem.  When fast
is true, threads can access the list ArrayList (using get()) without
locking.  Simultaneously, another thread can be adding items to the list.
Synchronizing in just the add() method is not sufficient.  Furthermore, it
may seem that since a temp variable is assigned the clone of list in the add
method, the new list will always be initialized properly, but based on what
I have read, this may not be the case.

Please refer to the Double-Check locking articles on Javaworld's site.  In
particular, refer to:
http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-double.html

Suggestion:
If you want to allow for unsynchronized access in the get() methods you
should not allow any changes in fast mode (i.e. prevent add, remove, etc. in
fast mode).

Please let me know of your thoughts.

Thanks

Vasanth


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>