Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

singleton blocking  RSS feed

 
Mark Lybarger
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've got a question about using the singleton pattern. say i have a class ServiceLocator that is implemented as a singleton (private constructor, has a single method to get to the instance of that class (getInstance()) ). it also has a method getEJB( String, class) that returns a remote component interface. say the getEJB method takes 5 seconds to complete and return. if i've got 100 simultaneous requests to that method on that class, are 99 of them going to be blocked waiting for the others to complete? or do they get their own instance of the method to execute?
i've checked out a similar situation where we were using static methods. it seems that each threads call to the static method gets it's own instance of the method to execute, thus there's no blocking for a public static method (non-singleton pattern).
thanks!
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you write a simple testprogram?
 
Mark Lybarger
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i wrote a test program, and it seems that there's no blocking on the methods in the singleton implementation. if there were, i'd expect to see "first outside - Singleton before", followed always by a "first outside - Singleton after " message:


 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct, a method can be executed simultaniously as often as you wish to - unless it is synchronized.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!