|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Empty interface stating that the implementing class is thread safe, typically by
means of synchronized methods. An object instance could then (by use of special
tools) be checked that it conceptually comprises a monitor [Hoare 1974], with some
differences that can be further avoided by using the Monitor
interface.
There are five ways of implementing thread-safe objects:
mailbox
monitor inside JThread.putEvent
.
Synchronized
, thereby permitting additional checking of the
code (by special tools).
Standard simple programming: Implement this (empty) interface, declare all methods synchronized, have attributes protected or private, and beware (for method implementations) that threads using other objects may run code that calls (and interferes with) wait and notify on this object.
Advanced programming:
In Java where the mutual exclusion of monitors are accomplished by the use of the keyword
synchronized
, whereas monitors as defined by Hoare are not explicitly
supported in the language or in the standard libraries (but the name of the
byte-codes refer to monitors). Due to the lack of language support,
external tools or test suits have to check that a class that implements Synchronized
really comprises a monitor, with some refinements due to object orientation.
Specifically, this means that the class implementing this interface should:
synchronized.
- Have no public or package-visible fields.
- Any protected (which implies package visibility) field may not be referenced
from within the same package.
- Methods that are synchronized, without being declared synchronized according to
the first item, should use an internal non-public object as an attribute for locking.
That is, with a private
lock
an unsynchronized method can contain
synchronized(lock){...}
, which prevents
wait/notify-interference from outside the monitor. A method that is synchronized
this way may not access monitor fields outside the scope of synchronized, but
local data and calls of other thread-safe methods are permitted. For example,
the following is also acceptable since arrival
will be allocated
on the stack of each calling thread and currentTimeMillis()
is
thread safe.
public void monitorMethod() {
long arrival = RTSystem.currentTimeMillis(); // This line is OK!!
synchronized (lock) {
// Using monitor attributes here....
}
}
- Any
static
non-final attributes may only be referenced by
static synchronized
methods, or by synchronizing (inside the
non-static monitor object method) on the class object for the entire scope
of the non-static method.
synchronized
is not inherited, is is very important that subclasses
of classes implementing Synchronized
also obey the items above, remembering
to implement each (virtual synchronized) method according to the rules above.
License
Nested Class Summary | |
static class |
Synchronized.Lock
|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |