Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

constructor

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can constructor be synchronized?
I know only method or blocks can be synchronized but constructor is also a type of method...can it also be synchronized??

Thanks,
Tashi
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No!
Constructor can't be synchronized. There is no any case where we
require constructor synchronized. Do you know any?


Thanks,
 
Tashi Rautela
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no..actually this was asked in one of the interviews...so was making it confirm...

Thanks,
Tashi
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a method is synchronized thread acquires a lock on current instance on which method was called.
In multithreaded environment there are chances that a same instance is used by multiple threads. so, synchronized is basically to prevent same resource being accessed by multiple threads simultaneously.

When we call constructor anyways we are creating a new instance. If multiple threads call the same constructor then they are working on different instances anyways, so there is no point in using keyword synchronized along with constructor.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


There is no any case where we require constructor synchronized



I also agree. But would there be a situation in a multi-threaded environment where in you need to synchronize the object creation? If at all, we have a pattern called Singleton for it.

Am i right?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Singleton Pattern, you want only one instance to be created, constructor
is private and very importantly the helper method that returns the
instance of the class must be made synchronized so that in multi-threaded
environment no two threads could access that method simultaneously otherwise
the method could end returning two different objects.

The shape of the Singleton class looks like:



Still cloning gotcha is there, to tackle with that, you should override
the clone() method and throw CloneNotSupportedException from that.


Thanks,
[ July 23, 2007: Message edited by: Chandra Bhatt ]
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raghavan Muthu:
But would there be a situation in a multi-threaded environment where in you need to synchronize the object creation? If at all, we have a pattern called Singleton for it.



As was said before, constructors do not need to be synchronized. Singletons have nothing to do with this.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally Posted by Ulf Dittmer:
As was said before, constructors do not need to be synchronized. Singletons have nothing to do with this.



Thanks Ulf and Chandra. As chandra said, its NOT the constructor but the static-helper method which should be synchronised in a multi-threaded environment.

Silly but serious mistake
[ July 23, 2007: Message edited by: Raghavan Muthu ]
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic