[concurrency-interest] question/bug in JCiP: Listing 4.12 ?

Joe Bowbeer joe.bowbeer at gmail.com
Wed Apr 7 18:27:35 EDT 2010


Safe publication of PVT is needed to guarantee visibility of the PVT
reference itself.

Regardless of safe publication, the immutable nature of the PVT data (e.g.
locations) guarantees that it is thread-safe.

See

http://jeremymanson.blogspot.com/2008/07/immutability-in-java-part-2.html
http://jeremymanson.blogspot.com/2008/04/immutability-in-java.html

Immutable in Java means "is transitively reachable from a final field, has
> not changed since the final field was set, and a reference to the object
> containing the final field did not escape the constructor".
>

Joe

On Wed, Apr 7, 2010 at 2:34 PM, Mike wrote:

> @Tim / @Joe - thanks for the (quite speedy...) reply.  And quickly,
> fantastic book, thank you; I own it & have gained significantly in both my
> java and design skills.
>
> Good to hear I wasn't crazy.  @Joe, I would tend to agree with the "why
> that when a safer one will do" sentiment - but many changes design
> improvements would be required to support 'real-world' functionality, so
> IMHO here I feel the example is muddled by the extraneous info.  And unless
> you fully understand the ramifications of your code, you can easily write
> complex sequential code accidentally...     Further, the sentence that
> follows the listing ("PublishingVehicleTracker derives its thread safety
> from delegation to an underlying ConcurrentHashMap") then seems to be
> incorrect - doesn't the PVT "delegate" to the SafePoints, not the CHM?
>
> So, as in my original post, it makes sense to me that a normal HashMap
> would technically be OK here.
>
> However, I am still uncertain with regards to visibility of the PVT's
> internals.  Somewhere in the heap the jvm has put a bunch of bits for the
> internals of the 'locations' map - String (key) and handle (value) pairs,
> and others.
>
> Is visibility not an issue here because the PVT meets all the criteria for
> an immutable object?  (not just an "effectively immutable one)
> -state cannot be modified (again, state is just key/value pairs here, which
> aren't ever modified)
> -all fields final
> -properly constructed
>
> Or if it is only "effectively immutable" (why?) then is visibility
> guaranteed if client code "safely publishes" it (ala JCiP page 52)?
>
> Thank you!
> -mboard182
>
>
> On Wed, Apr 7, 2010 at 11:28 AM, Joe Bowbeer wrote:
>
>> PS - the java.util folks have a different phrase...
>>
>> On Apr 7, 2010 11:26 AM, "Joe Bowbeer" wrote:
>>
>> To rephrase your question: Can the locations map in PVT be implemented as
>> a HashMap instead of a CHM, since it is read-only? I think the answer is
>> yes. But as we in the java.util.concurrent business say: Why use a HashMap
>> when a ConcurrentHashMap will suffice?
>>
>>
>> >
>> > On Apr 7, 2010 10:26 AM, "Mike" wrote:
>> >
>> > Hi,
>> >
>> > In working through Ja...
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20100407/2295e9db/attachment.html>


More information about the Concurrency-interest mailing list