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

Note that even if the compiler lets you call these methods anywhere in the<br />

program (since they are public methods of class Object), they only work if<br />

the calling thread first has locked the object via synchronized. Otherwise, an<br />

IllegalMonitorStateException is thrown.<br />

Be aware of the big difference between calling wait (i.e., Object.wait) and<br />

calling sleep (i.e., Thread.sleep) from inside a monitor, that is, from inside<br />

a synchronized method or block. Calling wait results in other threads being<br />

permitted to enter the monitor, like temporary unlocking to let some other<br />

thread change the state of the monitor as desired. Calling sleep means that<br />

the monitor is locked during the sleep time, and no other threads are allowed<br />

to enter during that time.<br />

Basic use of wait and notify<br />

A notified thread is moved from the condition queue to the monitor queue<br />

where it has to compete with other threads, as decided by the underlaying<br />

runtime system (here we assume priority ordered queues). When the thread<br />

holding the lock/monitor leaves, the first thread in the monitor queue is unblocked<br />

(put in the ready queue) and is subject to CPU time scheduling.<br />

Implications:<br />

• No matter how low priority or what OS, a thread that got into the monitor<br />

will complete its monitor operation (critical section) if given CPU<br />

time and if not calling wait. We say there is no resource preemption. 3<br />

• Due to preemption (that is, execution preemption, not to be confused<br />

with the resource preemption), the thread holding the monitor will remain<br />

there if higher priority threads are scheduled for execution instead.<br />

• There is nothing saying that a thread that has been notified, which<br />

means it has been in the exclusive are already, is given access again<br />

before any of the newly arrived threads in the monitor queue. This<br />

depends on the scheduler, however, and is not specified by Java.<br />

The major point now is that when a thread is continuing after wait, the condition<br />

that was waited for still cannot be assumed to actually be fulfilled. As<br />

an example, assume one thread calling obj = buffer.fetch() to get the next<br />

object from a buffer, and that the buffer turned out to be empty. A consumer<br />

thread calling fetch should of course be blocked until some other (producer)<br />

thread puts another object in the buffer by calling buffer.post(obj). Correspondingly,<br />

a producer thread calling post is to be blocked until the buffer<br />

3 In the original version of the monitor concept, proposed by Hoare in 1974, the notified<br />

thread did actually preempt the thread owning the monitor. Therefore, notify was only to<br />

be called as the last thing within the monitor operation. No such system is known to exist<br />

today.<br />

76 2012-08-29 16:05

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

Saved successfully!

Ooh no, something went wrong!