简体   繁体   中英

Multiple threads writing in a collection concurrently

I am having a scenario, in which lot of threads needs to share a collections and the threads keep on writing in the collection, comparatively writes are many and reads are very less, I am not sure ArrayBlockQueue is a correct collection to be used. Should I implement my own collection or do I get anything out of the Box in Java?

Please help.

I suggest you read the code for ArrayBlockingQueue and Disruptor first. If you think you can do it much better, you might write your own, but it is highly likely that writing your own won't be as performant, or well tested.

Generally systems that adhere to the "single writer principle" can obtain better performance. The concurrent collections do support multiple writers, but if instead you let each thread write to their own collections and the reader goes over each of them you'll likely get less contention and better throughput.

It depends on what do you look for "collection".

ArrayBlockingQueue as its name suggested is a Blocking Queue. If you simply want a collection to store bunch of data and read it, without using those blocking put/take operations, ArrayBlockingQueue is probably nothing different from a synchronized ArrayList. Think again what you are using the collection for.

Java provided some lock-free collections, like ConcurrentLinkedQueue. If you are not looking for a BlockingQueue, these collections may offer you better performance.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM