java - Thread safe list that only needs to support random access and appends -
i want have list of objects satisfies few basic requirements. needs support fast random access , needs safe use multiple threads. reads dominate far , ideally should fast normal arraylist
access, i.e. no locking. there no need insert elements in middle, delete, or change value @ index: mutation required able append new element end of list. more caller specify index @ element should placed, , index expected few more length of list, i.e. list dense. there no need iteration.
is there supports in java? can in third party library.
if not thinking implement own class. there'll internal array of arrays, each twice big last. lookups index little more maths figure out array has right element , index in array is. appends similar unless go beyond available space, in case new array allocated. creation of new array require lock acquired.
does sound sensible approach?
this doesn't sound particularly novel data structure. have name?
reads dominate far , ideally should fast normal arraylist access, i.e. no locking.
copyonwritearraylist works in scenario because cost of insertion amortized on large number of cheap read accesses.
under condition append-only 1 amortize further pre-sizing array , maintaining separate length , bumping atomically after insert.
other approaches necessary if you're concerned peak latency inserts. that's not 1 of criteria mentioned.
you have keep in mind you're asking data structure tailored use-case (append-only, lock-free, o(1) access, etc. etc.) whereas jdk provides general-purpose data structures make tradeoffs cover more use-cases.
there 3rd-party libraries provide more specialized implementations limited use-cases.
the type of datastructure describe spined buffer , used internally jdk in places (e.g. in form of java.util.stream.spinedbuffer<e>
), implementation not thread-safe , not exposed since not implement collection apis.
its javadocs state:
one or more arrays used store elements. use of multiple arrays has better performance characteristics single array used arraylist, when capacity of list needs increased no copying of elements required. beneficial in case results traversed small number of times.
i.e. it's useful write-once, read-a-few-times scenarios allocation costs dominate.
in read-heavy data-structures cost of indirection, math operations , non-sequential memory access might outstrip cost of occasional copying/reallocation.
Comments
Post a Comment