Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #14637
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: multithreaded cache? |
| References | <zsWdnVufD87Egy_SnZ2dnUVZ7oednZ2d@brightview.co.uk> <a1k06fFtdtU1@mid.individual.net> <4fb69812$0$6849$e4fe514c@news2.news.xs4all.nl> <a1nu7sFnnpU1@mid.individual.net> <jp6iji$n6m$1@dont-email.me> |
| Message-ID | <J%Ftr.22162$TC4.17002@newsfe14.iad> (permalink) |
| Date | 2012-05-18 22:15 -0700 |
On 5/18/12 3:31 PM, Eric Sosman wrote: > On 5/18/2012 5:45 PM, Robert Klemme wrote: >> On 18.05.2012 20:42, Silvio Bierman wrote: >>> On 05/17/2012 11:54 AM, Robert Klemme wrote: >> >>>> I provide a variant of Silvio's, Eric's and Daniel's solution which >>>> should yield higher throughput because it works without read write >>>> locking. You can find it as gist in case the code is garbled in the >>>> newsgroup posting: >>>> https://gist.github.com/2717818 >> >>> I think you have as many locks as I suggested (being one)? My initial >>> implementations of something like this used a plain map with an extra >>> lock but later cases used the by then available ConcurrentHashMap as >>> well, making one lock redundant. >> >> You didn't show it here, did you? I can's seem to find it in the thread. >> Note that CHM does also do synchronization. I am not sure from your >> statement what exact locking scheme you apply. There does seem to be one >> difference though: I my version the second lock goes away after the >> value has been computed so there is only the sync of CHM left. > > It seems to me that if N threads query the same key at about > the same time, they may all miss the map and go off to perform > the slow computation. Nope, they will all create a "Reference" object that *can* perform the calculation, however because the method uses "putIfAbsent", only the first calculating "Reference" object is actually ever used. After that, the Reference.get() method will block until that particular calculation is complete. Once that calculation is done, the calculating Reference will replace itself with a constant Reference for future accesses to use. > If "slow" is large compared to the cost of > a lock-release pair (and if it weren't, why cache?), the tradeoff > seems suspect. It would be suspect, but there isn't a tradeoff here. All threads lock until one value is calculated, basically coalescing the requests for that value. > > Also, different threads may wind up using different value > instances. If the cache is purely a convenience for a value-only > object that may be all right, but it's not all right if the values > are supposed to be singletons. Again, invalid if you recognize what is actually happening. > > Finally, there's more than a whiff of the double-checked locking > antipattern about what you're doing with the `here' flag. I'm not > absolutely sure what you've got is in fact DCL (hence, wrong), but > I'm also not absolutely sure it's DCL-free. Before using the pattern > in any important way, I'd want to check with a major-league guru, > just as "due diligence." > "double-checked" locking is only broken if you don't have the appropriate memory semantics enforced elsewhere. The reason the "classical" double-check locking fails is because there could be a data-race condition, where the reference is not null, but the object state hasn't been flushed between threads. In this case, the "first-check" of obtaining a value from the ConcurrentMap causes happens-before relationships, and causes the object state to flush. Robert Klemme's code is correct, according to everything I've read in JCIP and elsewhere. Cheers, Daniel.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
multithreaded cache? bugbear <bugbear@trim_papermule.co.uk_trim> - 2012-05-15 10:14 +0100
Re: multithreaded cache? Silvio Bierman <silvio@moc.com> - 2012-05-15 11:41 +0200
Re: multithreaded cache? Roedy Green <see_website@mindprod.com.invalid> - 2012-05-15 15:57 -0700
Re: multithreaded cache? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-05-15 16:13 -0700
Re: multithreaded cache? Silvio Bierman <silvio@moc.com> - 2012-05-16 01:22 +0200
Re: multithreaded cache? Silvio Bierman <silvio@moc.com> - 2012-05-16 01:19 +0200
Re: multithreaded cache? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-05-15 20:26 -0400
Re: multithreaded cache? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-05-15 21:41 -0300
Re: multithreaded cache? Silvio Bierman <silvio@moc.com> - 2012-05-16 09:26 +0200
Re: multithreaded cache? "Mike Schilling" <mscottschilling@hotmail.com> - 2012-05-26 14:54 -0700
Re: multithreaded cache? Roedy Green <see_website@mindprod.com.invalid> - 2012-05-16 22:03 -0700
Re: multithreaded cache? Silvio Bierman <silvio@moc.com> - 2012-05-21 15:25 +0200
Re: multithreaded cache? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-05-15 08:22 -0400
Re: multithreaded cache? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-05-15 09:56 -0700
Re: multithreaded cache? bugbear <bugbear@trim_papermule.co.uk_trim> - 2012-05-16 14:33 +0100
Re: multithreaded cache? Robert Klemme <shortcutter@googlemail.com> - 2012-05-17 11:54 +0200
Re: multithreaded cache? Robert Klemme <shortcutter@googlemail.com> - 2012-05-17 12:06 +0200
Re: multithreaded cache? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-05-17 09:51 -0700
Re: multithreaded cache? Robert Klemme <shortcutter@googlemail.com> - 2012-05-17 20:48 +0200
Re: multithreaded cache? Sebastian <sebastian@undisclosed.invalid> - 2012-05-20 01:35 +0200
Re: multithreaded cache? Sebastian <sebastian@undisclosed.invalid> - 2012-05-20 01:48 +0200
Re: multithreaded cache? Lew <noone@lewscanon.com> - 2012-05-19 18:11 -0700
Re: multithreaded cache? Silvio Bierman <silvio@moc.com> - 2012-05-18 20:42 +0200
Re: multithreaded cache? Robert Klemme <shortcutter@googlemail.com> - 2012-05-18 23:45 +0200
Re: multithreaded cache? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-05-18 18:31 -0400
Re: multithreaded cache? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-05-18 22:15 -0700
Re: multithreaded cache? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-05-19 07:09 -0400
Re: multithreaded cache? Robert Klemme <shortcutter@googlemail.com> - 2012-05-19 12:33 +0200
Re: multithreaded cache? Lew <noone@lewscanon.com> - 2012-05-19 14:24 -0700
Re: multithreaded cache? Robert Klemme <shortcutter@googlemail.com> - 2012-05-21 21:15 +0200
Re: multithreaded cache? Lew <noone@lewscanon.com> - 2012-05-23 23:11 -0700
Re: multithreaded cache? Robert Klemme <shortcutter@googlemail.com> - 2012-05-24 23:05 +0200
Re: multithreaded cache? Gene Wirchenko <genew@ocis.net> - 2012-05-24 20:11 -0700
Re: multithreaded cache? Robert Klemme <shortcutter@googlemail.com> - 2012-05-25 17:42 +0200
Re: multithreaded cache? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-05-25 07:09 -0300
Re: multithreaded cache? Silvio Bierman <silvio@moc.com> - 2012-05-21 15:21 +0200
Re: multithreaded cache? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2012-05-24 23:22 -0700
Re: multithreaded cache? Robert Klemme <shortcutter@googlemail.com> - 2012-05-25 17:36 +0200
Re: multithreaded cache? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2012-06-02 12:27 -0700
Re: multithreaded cache? Robert Klemme <shortcutter@googlemail.com> - 2012-06-02 21:54 +0200
csiph-web