commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kumar, Vasanth" <>
Subject Question about FastArrayList
Date Mon, 26 Nov 2001 20:07:08 GMT
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.

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

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:

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.



To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message