What is the best way to atomic-replace all contents in List in Java? -
suppose have list
role of cache. time list read-only buy every few seconds want atomic replacement of list contents.
in atomic mean don't want allow cache clients hit read between example clear()
, addall()
.
what list implementation use , how perform replacement best performance?
it better replace list contents or replace reference value itself?
java has readwritelock supports reads concurrently , writes exclusively. mentioned in javadoc, choice if updates occur not , reads occur often. faster writer updates list
better performance get.
the methods readlock() has called readers , writelock() writers. have call lock() on lock obtained. if available, tread continue working, otherwise block, until lock available.
use fairness when constructing readwritelock
enable reader , writer threads obtain locks in order requested it. otherwise thread wait forever (in worst case scenario).
the benefit of readwritelock
many reader may share same lock without obtaining it, expansive operation. benefit observable if ratio between reading , writing heavily in favor of reading.
Comments
Post a Comment