• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singleton in a servlet

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to use a class which implements singleton in my servlet.
Here is what i was tring to do:



I call the above class from doGet like this:



and when I refresh it, I always get the same number.
If i understood correcty each request makes a new thread to run, which calls service()->doPost/Get. There is always 1 instance of a servlet.
So if the above statemen is correct shouldn`t it always generate a new instance of Start class(in a new thread), so that i would benefit from singleton only if I would like to instantiate more then 1 Start class in the same thread/request. Does it have something to do with garbage collector?

Thanks in advance.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No; Start is a singleton--there's only one. That's what a singleton is. By what mechanism would a new Start be created?
 
Luke Kamble
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:No; Start is a singleton--there's only one. That's what a singleton is. By what mechanism would a new Start be created?



It is created when a request is made.



Start init = Start.getStart(); // gets the instance
Doesn`t the above statemant load a class and then initiate a fresh object of type Start whenever it is first called from a thread/request?. and if I would make a new object like this "Start init2 = Start.getStart();"(in the same request/thread) i would get the same reference as init.
Shouldnt I get with each new request a new Start object, and in the same request i shouldn`t be able to make the same object twice?
Or this is not how it works?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. "Start.getStart()" loads the Start class only once, the first time you refer to it. That's what it means to be a singleton. Remember the JVM doesn't go away after your request is completed.

If you want a different instance every time you call the getStart() method then just don't use a Singleton. Just create a new Start object.
 
Luke Kamble
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:No. "Start.getStart()" loads the Start class only once, the first time you refer to it. That's what it means to be a singleton. Remember the JVM doesn't go away after your request is completed.

If you want a different instance every time you call the getStart() method then just don't use a Singleton. Just create a new Start object.



Thanks alot, now i get it! :)
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, by the way!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic