JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
JAVA-BASED REAL-TIME PROGRAMMING
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
3. Multi-Threaded Programming<br />
3.3 Objects providing mutual exclusion – Monitors<br />
The key idea with the concept of monitors is to combine object oriented programming<br />
with mutual exclusion. As described in Chapter 2, an object with<br />
methods that are mutually exclusive is called a monitor. Actually, the monitor<br />
concept as initially defined by Hoare in 1974 was only based on abstract data<br />
types, but classes as in Java accomplishes that and more. Monitors do in principle,<br />
however, require the mutual exclusion to be implicit. That would mean,<br />
in Java, that the class defining the monitor should be tagged to comprise a<br />
monitor (for instance by using a keyword monitor preceding the class), which<br />
should imply mutual exclusion between all methods and all fields being protected.<br />
For performance reasons, that is not the way monitors are supported<br />
in Java. Instead, the following applies.<br />
3.3.1 Synchronized<br />
When threads concurrently call methods of an object, the calls are asynchronous<br />
since they can occur independently without any synchronization.<br />
When, on the other hand, calls are to be mutally exclusive (as for the bank<br />
account) we must restrict concurrent calls so that they are not asynchronous<br />
with respect to each other. In other words, methods need to be called synchronous,<br />
which in Java is accomplished by declaring them as synchronized.<br />
Synchronized methods<br />
All methods that should be mutually exclusive has to be declared synchronized.<br />
Methods that are not synchronized, as well as the data fields (or attributes),<br />
only obeys the usual protection rules and are from a concurrency<br />
point of view depending on programming discipline. It could be considered<br />
as a design flaw of the Java language that a class as such cannot be declared<br />
synchronized. Such a solution could have ment that all methods are implicitly<br />
synchronized and only synchronized methods can access the attributes. But,<br />
as mentioned, this is not the case in Java; you have to be aware about how<br />
concurrent access can be made and implement classes so that they are thread<br />
safe (reentrant methods and mutual exclusion wherever multiple threads can<br />
call) if that is required.<br />
A design decision not to make an object/package/library/application thread<br />
safe should be well documented. One such example is the so called Swing<br />
graphic packages (javax.swing.*) which is basically not thread safe; having<br />
all those methods synchronized would be a waste of GUI performance since<br />
almost all calls are made sequentially by a single thread anyway, which is accomplished<br />
by converting the concurrently occurring user actions into EventObject:s<br />
that are buffered and processed one at a time. Event processing<br />
66 2012-08-29 16:05