06.08.2013 Views

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

SHOW MORE
SHOW LESS

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 />

Monitors versus semaphores<br />

The monitor concept can be accomplished in different ways. In Java, monitors<br />

are not exactly supported, but synchronized methods (and a few simple rules)<br />

let us conveniently accomplish monitors. In practice, we may say that Java<br />

provides monitors, as well as possibilities to avoid (by omitting synchronized)<br />

the overhead of object locking when we (for sure) can deduce that the program<br />

will be correct even without locking. Anyhow, having synchronized built<br />

into the programming language is of great value. In languages without such<br />

support, we have to use library functions in combination with programming<br />

conventions.<br />

Monitors and semaphores have equal expressive power; a program using on<br />

of the mechanisms can always be rewritten using only the other mechanism,<br />

but it can be hard in practice.<br />

• Since a semaphore class is easily implemented by a monitor (providing<br />

the methods take and give) it is usually straight forward to rewrite the<br />

program using only monitors. However, if an interrupt routine (a static<br />

method without arguments that is connected to a hardware interrupt) is<br />

to call give (e.g. to signal that data is available in a hardware buffer), we<br />

cannot use an implementation that is based on synchronized (since an<br />

interrupt cannot be blocked even if the synchronized object is locked).<br />

• Implementing monitors using semaphores is troublesome when wait/notify<br />

is used. The problem is that the waiting thread has to take one<br />

semaphore (to be used for signaling when the condition is fulfilled) and<br />

give one semaphore (the mutex to let other threads access shared data)<br />

atomically as one operation. The solution is to let each thread have its<br />

own signaling semaphore.<br />

Clearly, both semaphores and monitors are needed, but in different situations.<br />

Polling locking state<br />

Since JDK1.4, class java.lang.Thread contains a method<br />

static boolean holdsLock(Object obj)<br />

that returns true if and only if the current thread holds the monitor lock on<br />

the specified object. Do not use this method, except for special cases such<br />

as test-cases where the locking of objects need to be verified. In particular,<br />

keep in mind that even if holdsLock returns one value, the opposite could be<br />

the case one machine instruction (of that thread, and a few context switches)<br />

later. Just like there (on purpose) are no methods for obtaining the state of<br />

a semaphore (without trying to acquire it), to avoid bad solutions on concurrency<br />

problems, you should not design your application such that holdsLock<br />

is needed.<br />

84 2012-08-29 16:05

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!