Origin: |
Grand 98 |
Reason: |
To prevent concurrent access to data or other resouces
(aka Critical Section).
|
Synopsis: |
Synchronize on a same object the portions of code that
should not be concurrently executed.
|
Example: |
Several threads set and get a datum held by an object.
The time of an access is non deteministic.
|
Solution: |
In its simplest form, setter and getter methods of a non-concurrent
resouce look like:
class HoldDatum {
private Datum datum;
public synchronized void set (Datum d) { datum = d; }
public synchronized Datum get () { return datum; }
}
|
A resource such as an I/O stream may be accessed by a
single synchronized method.
|
See also: |
This pattern is used by other "temporal" patterns.
|