Win a copy of Rust Web Development this week in the Other Languages 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Synchronisation and SIngleton

 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Gurus..
I was asked what will hapen if we are executing a singletone class and another request comes ? I said it will be made to wait. then I was asked why not to prefer synchronistion then ?

can anyone help ? what is the diff between having a class in singleton pattern and synchronising a class. ?
thanks in advance..
Leena
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
singleton means there is only one instance of sigleton class in lifetime of your JVM. synchronized your class does not prevent it having multiple instance in your JVM.
singleton pattern gurantee you can only get resource from same source.
I wish it would help.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Singleton is used when we want to create only one instance of an object. Such as in case of SessionManager where there should be only one instance which stores various session objects.
Synchornized keyword is used when there are simultaneously many requests are there to an object and instead of have deadlock or something the request should be handled one at a time. In a synchronization when the first request is called lock is made on the object and other request have to wait till the process gets finished.
So in singleton classes all the public methods are synchronized so that one request at a time is handled.
Please correct me if i m wrong.
Puja
 
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