Are Singleton methods thread safe ?

A

Anand

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,
 
V

VisionSet

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?
 
S

Stefan Schulz

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.
 
O

Oliver Wong

Anand said:
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?

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
 
A

Anand

Thanks Stefan.
- I mean calling a method from a singleton class.

BTW what is reentrant method?
 
M

Monique Y. Mudama

Thanks Stefan.

- 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.
 
J

Jacob

Anand said:
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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top