A Synchronized method provides a guarantee that the method is the only synchronized method in the object running at the time.
|
This is useful if the resource is contained completely in the object, and in general, no waiting for a resource is necessary. (The operation is contained completely in the method)
-
public synchronized void do_it() {
-
}
|
More fine-grained synchronization can be obtained by synchronizing on the particular object:
-
Object obj;
-
public void do_it() {
-
synchronized(obj) {
-
... change the state of obj ...
-
}
-
... do other time-consuming stuff ...
-
}
|