• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

syncrhronization confusion

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I was reading the sun tutorial about thread synchronization from the following link.

http://java.sun.com/docs/books/tutorial/essential/concurrency/syncmeth.html

Here it says that synchronizing constructors is a syntax error. Then it presents a warning like this and this statement i am unable
to understand. I am copying the statement here.

Warning:
When constructing an object that will be shared between threads, be very careful that a reference to the object does not "leak" prematurely. For example, suppose you want to maintain a List called instances containing every instance of class. You might be tempted to add the line
instances.add(this);
to your constructor. But then other threads can use instances to access the object before construction of the object is complete.


What does the last line trying to say. But then other threads can use instances to access the object before construction of the object is
complete? Can someone explain me this?

Ben
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object is constructed completely only when its constructor completes. If you allow other threads access to the object before the constructor completes, the threads might actually be working with an incomplete instance of the object, which could lead to problems.

Does that help ?
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Umm i am gonna ask a very silly question here Deepak how does

instances.add(this)

prevent a construct to complete its construction?
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Onething more i want to know what does it really mean to interleave the effect of the fields?
What does the keyword "interleave" means?
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Zaidi wrote:Umm i am gonna ask a very silly question here Deepak how does

instances.add(this)

prevent a construct to complete its construction?



It does not prevent the completion of the constructor. However if another thread had access to the instances collection, it can access this object before the constructor completes.

what does it really mean to interleave the effect of the fields?



I am not sure in what context it was used. Can you quote the text and a few lines above / below it so that the context is clear ?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic