Reputation: 57
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.
Upvotes: 1
Views: 1134
Reputation: 40036
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.
Upvotes: 1
Reputation: 111229
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.
Upvotes: 2
Reputation: 533492
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.
Upvotes: 1