Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Are Singleton methods thread safe ?

Thread view: 
Anand - 09 Feb 2006 17:36 GMT
Are Singleton methods thread safe ?

Let's assume that multiple threads are accessing singleton class
method. (method is not synchronized). There are no class level
variables and there are only method level variables. Is this going to
be an issue or the methods are thread safe?

If it is safe? how?

Thanks,
VisionSet - 09 Feb 2006 17:39 GMT
> Are Singleton methods thread safe ?
>
> Let's assume that multiple threads are accessing singleton class
> method. (method is not synchronized).

> There are no class level variables

That is the important bit, it doesn't matter a jot about it being a
singleton.

Every caller gets there own copy of local variables in a method.

Thread safety means no corruption of data, ie one thread doesn't change
something another thread may read, unless it is intended in a controlled
manner that the programmer has coded for through proper synchronisation.

So if there are no class level variables (we call these attributes or
instance variables) then what is there to corrupt?

> and there are only method level variables. Is this going to
> be an issue or the methods are thread safe?
>
> If it is safe? how?

--
Mike W
Stefan Schulz - 09 Feb 2006 17:41 GMT
I am not quite sure what you mean by "singleton method". A method is
reentrant (which is a bit stronger then most definitions thread safe)
if it does not modify any state, that is, if it is free of
side-effects. If your method can claim that, then yes, it is
thread-safe even without explicit synchronization.
Anand - 09 Feb 2006 17:56 GMT
Thanks Stefan.

>>"I am not quite sure what you mean by "singleton method". "
- I mean calling a method from a singleton class.

BTW what is reentrant method?
Oliver Wong - 09 Feb 2006 18:05 GMT
> Thanks Stefan.
>
>>>"I am not quite sure what you mean by "singleton method". "
> - I mean calling a method from a singleton class.
>
> BTW what is reentrant method?

See http://en.wikipedia.org/wiki/Reentrant

   - Oliver
Monique Y. Mudama - 09 Feb 2006 18:25 GMT
> Thanks Stefan.
>
>>>"I am not quite sure what you mean by "singleton method". "
> - I mean calling a method from a singleton class.
>
> BTW what is reentrant method?

Stefan defined it in the post in which he mentioned it.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Roedy Green - 10 Feb 2006 04:25 GMT
>BTW what is reentrant method?
All Java methods are reentrant. It means several threads can be
executing the same code at once.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 10 Feb 2006 04:41 GMT
>BTW what is reentrant method?

see http://mindprod.com/jgloss/reentrant.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 09 Feb 2006 17:52 GMT
> Are Singleton methods thread safe ?
>
[quoted text clipped - 4 lines]
>
> If it is safe? how?

   I'm not familiar with your terminology of "method level variables" and
"class level variables". There's 3 types of variables to be concerned with
here: static fields, instance field, and local variables.

   If you've got static fields, then you might have threading problems.

   If you've got instance fields, then you might have threading problems.

   If you only have local variables, then you should be okay.

   - Oliver
Anand - 09 Feb 2006 18:02 GMT
Thanks Oliver.

>> I'm not familiar with your terminology of "method level variables" and "class level variables".
- I meant local variables as method level variables.
Jacob - 10 Feb 2006 07:47 GMT
> Are Singleton methods thread safe ?
>
[quoted text clipped - 4 lines]
>
> If it is safe? how?

Make sure you make the instance variable itself final. Using lazy
initialization at the first getInstance() request might seem like
a good idea, but is not thread-safe. Use this pattern:

  private MySingleton
  {
    private static final MySingleton instance_ = new MySingleton();

    public static MySingleton getInstance()
    {
      return instance_;
    }

    private MySingleton()
    {
    }
  }

The other methods must be synchronized according to their class
scope variable usage, and since you don't have any, there are no
further issues.
Jacob - 10 Feb 2006 07:47 GMT
>   private MySingleton
>   {

Oops, of cource:

  public class MySingleton
  {

Sorry :-)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.