Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Java Data Structure for Fast, Concurrent Insertions

My use case is as follows: I have 10 threads simultaneously writing to one data structure. The order of the elements in the data structure does not matter. All the elements are unique. I will only be doing a read from this data structure only once at the very end.

What would be the fastest native Java data structure to suit this purpose? From my reading, it seems Collections.synchronizedList might be the go to option?

like image 580
John Roberts Avatar asked Mar 14 '23 12:03

John Roberts


1 Answers

I have 10 threads simultaneously writing to one data structure.

I think it would be best to use a separate data structure per thread. That way no synchronisation is needed between the threads, and it would be much more CPU cache friendly too.

At the end they could be joined.

As for the underlying structure: if the elements are fixed size, an array/verctor would be best. Joining them would only take a copy of the block of memory they occupy, depending on the implementation - but lists would always be slower.

like image 123
Danny_ds Avatar answered Mar 24 '23 05:03

Danny_ds