0

I have a shared collection an ArrayList and also i use a ReentrantReadWriteLock lock to secure the entering on a critical area from different threads. My threads are three a writer,read,delete thread. i acquire the correct lock on each case. The logic is that i insert data to ArrayList, read them when necessary, and also when the timer reaches limits delete some entries. The process runs smoothly and everything is perfect.

My question now is can i transfer the above logic somehow and implemented it with an LMAX disruptor in order to avoid lock overheads and improve performance. If yes can you describe me an ideal case and if you are able to also post code i would really appreciate it.

i assume that instead of ArrayList data will be entered in ringbuffer and i will have 2 producers write, delete, and a consumer for read. Also i must make sure that i use producer barriers. Will the performance will be increased from lock case. i am not sure if i understand everything correctly please help me and give me directions?

Mixalis Navridis
  • 181
  • 2
  • 15
  • How many different types of readers are there? – Slugart Jun 27 '22 at 04:15
  • @Slugart i will have 2 producers write, delete, and a consumer for read – Mixalis Navridis Jun 27 '22 at 09:46
  • What is the access pattern for the reads and writes in the ArrayList. I.e. does the reading thread scan the whole list periodically or just read new elements, or read randomly in the ArrayList? Do the writes update elements in place or just append to the end of the list? Do the deletes take place at the beginning or end or anywhere within the ArrayList? What are the relative costs between reading, writing and deleting? – Slugart Jun 28 '22 at 00:43
  • @Slugart Well let me answer to all of your questions. The reading thread scans the whole list. The writter thread always append to the end of the list. The delete thread takes place anywhere base on a search value. For the relative cost i am not sure i understand it clearly. I hope i help please give me your valuable feedback – Mixalis Navridis Jun 28 '22 at 07:39
  • The relative cost is for exmample: processing takes much more time to execute than writing or deleting. – Slugart Jun 28 '22 at 12:04

1 Answers1

0

If your shared state is the ArrayList and you have one thread that is reading and processing the elements in the ArrayList and you want the updates to that shared state synchronised then usually the ArrayList would be owned by one EventHandler that processes events such as Process, Write, Delete and updates and processes the shared state.

This would all run on one thread but that pretty much is what is happening now as you cannot Read at the same time as Writing/Deleting.

As you only have one reading thread there is not much to be gained from using a ReadWriteLock as you will never have concurrent reads.

Slugart
  • 4,535
  • 24
  • 32
  • you describe a case that i have only one thread reading i told you writte, delete also can run simultaneously – Mixalis Navridis Jun 29 '22 at 08:44
  • and having only one event handler for both process,Write, Delete is not option to my protocol also its totally useless use disruptor with one eventhandler – Mixalis Navridis Jun 29 '22 at 08:50
  • A ReadWriteLock Will allow concurrent reads but not concurrent writes (a delete is a write). From the spec “ ReentrantReadWriteLocks can be used to improve concurrency in some uses of some kinds of Collections. This is typically worthwhile only when the collections are expected to be large, accessed by more reader threads than writer threads”. If you only have one reader thread then all your operations are effectively synchronised. – Slugart Jun 29 '22 at 13:10
  • is it bad to have one eventhandler for each of my cases for example separate event handlers read,write and delete? – Mixalis Navridis Jun 29 '22 at 13:49
  • They cannot modify the same shared state. You can pass messages between event handlers by adding information to the events and sequencing the event handlers so that one processes events after the other but that is the only thread-safe way to pass data between the event handlers. – Slugart Jun 29 '22 at 13:58