• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

I read in Java Concurrency in Practice that ....

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Safe publication is sufficient for other threads to safely access objects that are not going to be modified after publication without additional synchronization.
The safe publication mechanisms all guarantee that the as-published state of an object is visible to all accessing threads as soon as the reference to it is visible,
and if that state is not going to be changed again, this is sufficient to ensure that any access is safe"

So if I've got



Is class B thread-safe ?
 
Saloon Keeper
Posts: 13396
296
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, a second thread that calls accessI() will always see the value 3.
 
Marshal
Posts: 74375
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aren't both those classes sort of immutable if you don't subclass them?
 
Stephan van Hulst
Saloon Keeper
Posts: 13396
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the point of the example Campbell. The class is thread-safe because it is immutable and published safely. If the 'a' field was not declared volatile (and also not final), B would NOT be thread-safe, even if it is immutable.
 
Michael Farragher
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess class B could be rewritten as



??
 
Stephan van Hulst
Saloon Keeper
Posts: 13396
296
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it could, but this would incur unnecessary overhead. The synchronized keyword is a heavier tool than the volatile keyword. Besides, you don't need to use the synchronized keyword in the constructor at all if you synchronize access to a in the accessI() method.
 
Stephan van Hulst
Saloon Keeper
Posts: 13396
296
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is not the point of your question, but as a final (hehe) remark I want to say that if you have an immutable class, you NEVER have to use volatile or synchronized, because you can use final instead and it will make your code faster as well.
 
Campbell Ritchie
Marshal
Posts: 74375
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that your final word, Stephan?
When I started learning Java® I found it hard to understand that there were two separate keywords final and finally.
 
Michael Farragher
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing that puzzles me in JCIP is when it talks about immutable objects. It says that immutable objects are immune to improper publication.

It gives this example :




In another class, you've got



It says this is publishing an object without adequate synchronization. But it says that making n in Holder final would make the class immutable and would then be immune to improper publication. But you'd still need to make the holder ref volatile - or employ anothre synchonization mechanism - so that reading threads would see the right object ?!!? In other words you'd need to publish it properly. Can someone resolve my confusion ?! Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 13396
296
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example given by JCIP is not about the safe publication of the reference to the Holder object, but rather the Holder object itself.

A class that is immutable can not guarantee anything about references to its objects, that's the responsibility of the classes that use the immutable class.

The assertSanity() method makes an assertion about n, not about holder.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic