• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Replacing Singleton class with Synchronized behaviour

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Can anybody please tell me

What s the need of creating Singleton classes , cant we achive the same thing using a Synchronized method ??
Sorry if its a dump question , but help me in knowing the answer .

Thanks .
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are comparing carrots and chickens here Singleton serve one purpose, which is basically to hold a single instance of an object. Concurrency issues have to be taken into consideration if many threads access a Singleton. A synchronized method does something, anything, and prevent multiple threads to execute it at the same time. You may have some synchronized methods in a singleton, to prevent concurrency issues mentioned previously.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The reason why i posted this question is taht , we basically create a Singleton class as we want a Single Point of Entry in the Application .

Singleton class for Single point of Entry in the Application .

Synchronized methods in a class : Single point of Entry in the class itself


Later after your response , it was like comparing carrots and chickens

Thanks .

 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a singleton to assure one point of entry. Then add synchronization to
enforce "single-file" access to its behavior (by multiple threads).

Jim ... ...
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Kiran V wrote:
Synchronized methods in a class : Single point of Entry in the class itself


Also search for static holder idiom, which is an efficient way to implement singleton.
 
reply
    Bookmark Topic Watch Topic
  • New Topic