java - Wait for read lock? -
how can wait lock before checking it?
basically, want cache list in private variable. populate list every once , while, other 99.999999% of time, want read it, don't want lock every time read.
public class someservlet extends customservlet {      private static object locker;     private static list<string> somelist;      // moderately heavy populate, not called     private void populatelist() {         // lock         somelist.clear();         somelist.addall(gettheliststuff());         // unlock     }      public void dogetlikemethod(httpservletrequest req, httpservletresponse res) {          // looking @ sort of method check lock           // , wait it, preferably timeout           if(!locker.islocked(1000) && somelist.isempty()) {              populatelist();          }          // lock present far less 0.01% of time checked     }      public void updatesomelist() {        populatelist(); // populate list other reason     } }   this in servlet , not using public framework. our lead protective of adding libraries, i'd avoid if @ possible. have apache , java.util stuff. i'm not sure if should use sort of sychronized, readwritelock, reentreadwritelock, or lock. 
i think explained enough. let me know if need clarify anything. may approaching entirely wrong.
use java.util.concurrent.locks.reentrantreadwritelock.  multiple threads can hold read lock @ time, long no write going on, satisfies efficiency desires.  single thread can hold write lock @ time, , when no threads hold read lock, ensures consistency between writes , reads.  want set fairness on, write threads able writes when there constant contention reads.
Comments
Post a Comment