Friday, 22 April 2016

How to publish an object safely ?


To publish an object safely :
  • Initialize an object reference from a static initializer
  • Store the reference in a volatile field or AtomicReference
  • Store the reference in a final field or properly constructed object
  • Store the reference into a field which is properly guarded by a lock

Using static initializer is the easiest and safest way to publish objects that are suitable to construct in static context.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.