java - setting servlet context attribute in a theadsafe manner -
i'm working on web application in servlet named checkurl read data xml file in init method , put node list xml in servletcontext. every request coming servlet not have read data xml again , again. node list servletcontext. there servlet in application used update xml , update servletcontext nodelist variable if xml updated.
so in scenario, have synchronization in place make sure that, when writing xml happens, reading servletcontext nodelist should blocked. if not might reading data not date.
to achieve this, wondering synchronization level should using. should synchronize on servletcontext object or should synchronizing on element set in servlet context or should synchronizing on servlet class instance.
synchronization scope recommended minimal possible performance reasons. less chunk of code takes less time execute , less time other threads wait.
on case should synchronize on nodelist level. can use collections.synchronizedlist
contain node list. synchronization work atomic operations. however, perform multiple operation on list 1 atomic operation need explicit synchronization. example, when read-and-write. code example here.
Comments
Post a Comment