I want to write a simple thread-safe arraylist which supports:
add(), remove(int i), insert(int i), update(int i), and get(int i)
One simple implementation is to add lock to the internal data structure(an object array for example), but it is not good enough because only one thread could access the list at a time.
Therefore my initial plan is to add lock to each data slot so that different threads could have access to elements in different indexes at the same time. The data structure will look like this:
class MyArrayList {
Lock listlock;
Lock[] locks;
Object[] array;
}
The locking should work as follows if there is no need to do resize():
My questions are:
A simple approach would be to just use a read-write lock ( [Reentrant]ReadWriteLock
), so many threads could read concurrently, but once someone gets the write lock, nobody else can access the list.
Or you could do something somewhat similar to your idea: one read-write lock for each slot + a global ("structural") read-write lock + a variable to keep track of the j >= i
cases. So:
int modifyingFrom
variable indicating all positions from there on are "locked" (the j >= i
cases). After setting modifyingFrom
, you downgrade (see docs ) from write to read lock, letting others access the list. modifyingFrom
. If there's a conflict, sleep until the thread who has set modifyingFrom
finishes and notifies everybody who is waiting. This check must be synchronized (just use synchronized (obj)
on some object) so the structure-changing thread doesn't happen to obj.notify()
before the conflicting thread calls obj.wait()
and sleeps forever (holding the global read lock!). :( boolean structuralChangeHappening = false
or set modifyingFrom
to some x > <list size>
when no structural changes are happening (then you can just check that i < modifyingFrom
to get()
or update()
). A thread finishing a structural change sets modifyingFrom
back to this value and here's where it has to synchronize to notify waiting threads. trimToSize()
or something) array would hold the global write lock during the entire operation . I was tempted to think the global read-write lock wasn't really necessary, but the last two points justify it.
Some example cases:
get(i)
(each with it's i
, unique or not): each one would get the global read lock, then the i
th read lock, then read the position, and nobody would wait at all. update([index =] i, element)
: if there are no equal i
s, nobody will wait. Otherwise, only the thread writing or the threads reading the conflicting position will wait. t
starts an insert([index =] 5, element)
, and other threads try to get(i)
: Once t
has set modifyingFrom = 5
and released the global write lock, all threads reading get the global read lock, then check modifyingFrom
. Those with i < modifyingFrom
just get the (read) lock of the slot; the others wait until the insert(5)
finishes and notifies, then get the lock of the slot. add()
and needs to allocate a new array: Once it gets the global write lock, nobody else can do anything until it has finished. t_a
calls add(element)
and another thread t_g
calls get([index =] 7)
:
t_a
happens to get the global write lock first, it sets modifyingFrom = 7
, and once it has released the lock, t_g
gets the global read lock, sees that index (= 7) >= modifyingFrom
and sleeps until t_a
finishes and notifies it. t_g
gets the global read lock first, it checks that 7 < modifyingFrom
( modifyingFrom > <list size> (== 7)
, 4th point before the examples), then throws an exception because 7 >= <list size>
after releasing the lock! Then t_a
is able to get the global write lock and proceeds normally. Remembering that accesses to modifyingFrom
must be synchronized.
You said you want only that five operations, but if you wanted an iterator, it could check if something changed by other means (not the iterator itself), like standard classes do.
Now, I don't know under which conditions exactly this would be better than other approaches. Also, consider that you may need more restrictions in a real application, because this should ensure only consistency: if you try to read and write the same position, the read can happen before or after the write. Maybe it would make sense to have methods like tryUpdate(int, E)
, that only does something if no conflicting structural changes are happening when the method is called, or tryUpdate(int, E, Predicate<ArrayList>)
, which only does its work if the list is in a state that satisfies the predicate (which should be defined carefully not to cause deadlocks).
Please let me know if I missed something. There may be lots of corner cases. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.