I have a manager as Spring wired bean. I believe every bean defined for spring by default is wired as singleton. I have some methods in this bean which I need to synchronize.
How should I be doing that then —
void zzz() {
synchronized (this) {
...
}
}
or
void zzz() {
synchronized (MyClass.class) {
...
}
}
?
The main difference in the two is that in the first case, the the instance of the class as the monitor and the second one uses the Class as the monitor.
The first one is probably the way to go in your case because, in the near future if you decide to have many instances of your class, their methods will be synchronized on the respective instances. As opposed to if you use a Class as a monitor, if one thread is calling a synchronized method on one instance, no other threads will be able to call methods (those that are synchronized) on any instances of the same class.