View Single Post
  (#1 (permalink)) Old
radkat radkat is offline
Member
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Feb 2008
Default synchronized keyword in singleton - 02-28-2008, 11:36 AM

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 ?
Reply With Quote