|
|
|
| |
Synchronized
Every Java object has a lock. A synchronized
block uses an object's lock to act like a binary
semaphore with the initial value "1",
solving the mutual exclusion critical section
problem:
Object obj = new Object();
...
synchronized (obj) { // in a method
... // any code, e.g., critical section
}
The construct:
... synchronized method(...) {
... // body of method
}
is an abbreviation for:
... method(...) {
synchronized (this) {
... // body of method
}
}
That is, the entire body of the instance method
is a synchronized block on the object (keyword
this) the method is in.
|
|