Implementing thread safe ArrayList in Java by locking

I want to write a simple thread safe ArrayList that supports:

Add(), remove (int i), insert (int i), update (int i) and get (int i)

A simple implementation is to add locks to internal data structures (such as object arrays), but it is not good enough because only one thread can access the list at a time

Therefore, my initial plan is to add locks for each data slot so that different threads can access elements in different indexes at the same time The data structure is as follows:

class MyArrayList {
    Lock listlock;
    Lock[] locks;
    Object[] array;
}

If you do not need to adjust resize (), locking should work as follows:

>For get (int i), the thread needs to obtain the lock [i]. > For insert (int i), the thread needs to obtain all locks of J > = I and listlock [J]. > For remove (int i), a thread needs to obtain all locks of J > = I and listlock [J]. > For add(), the thread needs to get listlock. > For insert (), the thread needs to acquire the lock [i]

My question is:

>How to handle locking when resizing more objects, I need to create a new larger array to hold all objects This is annoying because some other threads may also wait to release the lock, > what better suggestions are there to implement this thread safe ArrayList?

Solution

A simple method is to use the read-write lock ([reentrant] readwritelock). So many threads can read at the same time, but once someone obtains the write lock, others cannot access the list

Or you can do something similar to your idea: a read-write lock global ("structure") read-write lock variable for each slot to track J > = I So:

>To access (read or write) anything, a thread needs at least a global read lock. > Only the thread trying to make a structure change (the thread changing the size) will obtain the global write lock, but only one int modifyfrom variable can be set, indicating that all positions from there are "locked" (J > = I) After setting modifyfrom, you will downgrade from write to read lock (see DOCS) and let others access the list. > If any thread attempts to perform any non structural changes, once it holds the global read lock, check whether the operation it wants to perform conflicts with the current value of modifyfrom If there is a conflict, please sleep until the thread with modifyfrom set is completed and notify all waiting people This check must be synchronized (only synchronized (obj) is used on an object), so obj is called on the conflicting thread The structure change thread will not occur in obj until wait() and Hibernate forever (keep global read) Lock in notify()

The above is all the contents of ArrayList that realizes thread safety by locking in Java collected and sorted by programming home for you. I hope this article can help you solve the program development problems encountered by locking ArrayList that realizes thread safety in Java.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>