View Single Post

Topic: synchronized keyword in singleton
Old 02-28-2008, 10:36 AM   #1 (permalink)
radkat
Member
 
Join Date: Feb 2008
Posts: 1
Good day, all.

I'm new to multi-thread and cannot emulate this issue properly, so if anyone could help by comment, I would appreciate much.

The question is, whether synchronized keyword is needed in non-static methods of singleton ?

E.g.

public class MySingleton {
private static MySingleton instance;

public static synchronized MySingleton getInstance() {
if (instance == null) {
instance = new MySingleton();
}
return instance;
}

public void myMethod() {
// do something
}
}

Does method myMethod need to be synchronized or not ?
radkat is offline   Reply /w Quote -