[concurrency-interest] Can this be done with ConcurrentHashMap ?

Joe Bowbeer jozart@blarg.net
Thu, 23 Dec 2004 16:40:47 -0800


--b1_351e013353a503c3b73aa5e9a85906b9
Content-Type: text/plain; charset = "iso-8859-1"
Content-Transfer-Encoding: 8bit

Donnie Hale <donnie@haleonline.net> wrote:
> Two follow-ups:
> 
> 1) In the code below, I want to make sure I understand what is supposed
> to happen in Memoize.compute, within the outermost "if (f == null)"
> block. It would seem that two threads could both get into this block
> if "cache.get(arg)" returns null. Assuming I've got that right, then we
> have 2 threads instantiating a FutureTask and attempting to put it in
> the map via "cache.putIfAbsent". However, only one of those threads
> will win; and the other will be returned the winning thread's
> FutureTask.

Right.

> Only the winning thread will run the FutureTask. Not being up-to-speed
> on Futures and FutureTasks, I'm guessing that even if the losing thread
> executes the line "return f.get()" first, it will not return until
> "ft.run()" completes in the winning thread?
> 

Right.


FWIW, The Memoizer was presented at JavaOne 2004 in TS-2136.

Also see MemoTest.java at 

http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/javaone/



--b1_351e013353a503c3b73aa5e9a85906b9
Content-Type: text/html; charset = "iso-8859-1"
Content-Transfer-Encoding: 8bit

Donnie Hale <donnie@haleonline.net> wrote:<br>> Two follow-ups:<br>> <br>> 1) In the code below, I want to make sure I understand what is supposed<br>> to happen in Memoize.compute, within the outermost "if (f == null)"<br>> block. It would seem that two threads could both get into this block<br>> if "cache.get(arg)" returns null. Assuming I've got that right, then we<br>> have 2 threads instantiating a FutureTask and attempting to put it in<br>> the map via "cache.putIfAbsent". However, only one of those threads<br>> will win; and the other will be returned the winning thread's<br>> FutureTask.<br><br>Right.<br><br>> Only the winning thread will run the FutureTask. Not being up-to-speed<br>> on Futures and FutureTasks, I'm guessing that even if the losing thread<br>> executes the line "return f.get()" first, it will not return until<br>> "ft.run()" completes in the winning thread?<br>> <br><br>Right.<br><br><br>FWIW, The Memoizer was presented at JavaOne 2004 in
TS-2136.<br><br>Also see MemoTest.java at <br><br>http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/javaone/<br><br>



--b1_351e013353a503c3b73aa5e9a85906b9--