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

by semaphores). When an object has been locked by synchronized (for a<br />

method or a blocks), there can be conditions that need to be fulfilled before<br />

the operations on the shared data can be fulfilled. Java provides the following<br />

support for that situation.<br />

3.3.2 Conditions – wait and notify<br />

Consider a buffer with operations put and get. To accomplish mutual exclusion<br />

during manipulation of the buffer, those methods are typically declared<br />

synchronized. However, both in this case as well as in many other types of<br />

monitors, certain conditions may need to be fulfilled before an operation can<br />

be completed. For instance, there must be something in the buffer to fetch<br />

before get can be performed, and the buffer may not be full in order to let put<br />

be carried out. If such conditions can be evaluated before any shared data is<br />

manipulated we could simply cancel the operation and make a new call when<br />

the status has changed, but we also require:<br />

• When waiting for a certain condition to be fulfilled, that thread should<br />

be blocked to prevent waste of CPU time (polling or busy wait, possibly<br />

leading to starvation of other threads, is not allowed).<br />

• When the condition is fulfilled we need a way of waking up the blocked<br />

thread(s).<br />

• When a thread continues execution after being blocked on some condition,<br />

it has to compete for the resource again since we have to ensure<br />

that only one thread at a time is executing inside the monitor (i.e., inside<br />

the critical region).<br />

To accomplish these features, each Java object is equipped with a wait/notify<br />

mechanism in terms of the methods wait, notify, and notifyAll according to<br />

(see Figure 3-7):<br />

wait() The thread executing within the monitor (inside a synchronized method<br />

or block) gives up the exclusive access, stepping out to the ’back yard’<br />

containing the condition queue. The runtime system puts the blocked<br />

thread in the condition queue, where it typically is inserted in priority<br />

order with the thread with the highest priority first. The saved context<br />

of the blocked thread, referring to the stack of the thread and thereby<br />

to the state of execution, of course contains the state of the half made<br />

monitor operation.<br />

notify() Threads waiting in the condition queue will remain there until notified<br />

by some other thread, or until a timeout occurs as explained later.<br />

Threads that inside the monitor change any data that affects conditions<br />

that the threads in the condition queue might waiting for, should call<br />

74 2012-08-29 16:05

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

Saved successfully!

Ooh no, something went wrong!