Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail NNTP-Posting-Date: Tue, 15 May 2012 04:14:01 -0500 Date: Tue, 15 May 2012 10:14:01 +0100 From: bugbear User-Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:12.0) Gecko/20120429 Firefox/12.0 SeaMonkey/2.9.1 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: multithreaded cache? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 39 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-xY0Ao3rlu5ujLh5s0RueTZfXYltWtDMtlnzEMTe7u+muRKTAZNNeNSFp16r/Yy6HAuph97Zd7qGeNKr!JGfFDCSSX53W/ftVzbz1eqltxbaVX4JShM1gXrnGjkqVZxdVojNIReLIbNbt7vko94FZDF57wMaG!jfXnUTphfU7y/tb3XgoejwaiJg== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2274 Xref: csiph.com comp.lang.java.programmer:14544 I'm using various Apache Commons Maps as a multithread cache, protected using ReentrantReadWriteLock, so that getting() uses a read lock, and putting() uses a write lock. But I've got an issue; in the case of a cache miss (protected by a read lock), the required value is acquired using the "underlying function" that the cache is over; this value is then put() into the cache (protected by a write lock) This is all perfectly thread safe, and gives correct results. However, if the underlying function is slow and/or resource hungry (consider cacheing a ray traced image!) many threads can end up calling the real function (second and subsequent threads to the first get a miss during the first threads call to the underlying function). "obviously..." what I want is for only the thread that FIRST has a cache miss calls the underlying function, whilst other threads (for the same key) wait. This seems "obvious" enough that I'm guessing there's a standard solution. Googling led me to several "heavy" libraries; This appears more a locking/cacheing issue than a Java issue, although my implementation is in Java. Can anyone (please) point me at a canonical solution/implementation? BugBear