06.08.2013 Views

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.4 Message-based communication – Mailboxes<br />

3.4. Message-based communication – Mailboxes<br />

Instead of the low-level characteristics of semaphores, the desire to support<br />

concurrency in an object oriented way led to monitors; passive objects used<br />

to implement collaboration between active objects. In many cases, however,<br />

we can benefit from having such passive objects with additional functionality<br />

for:<br />

Buffering: Often threads work in producer-consumer like situations when<br />

transmitted objects need to be buffered in some type of queue. That lets<br />

the threads work more asynchronously with respect to each other. That<br />

is, even if there are some temporary blocking when posting/fetching<br />

objects into/from the queue (to accomplish mutual exclusion during the<br />

assumably short queuing operations), a buffer overall lets the threads<br />

operate more independently of each other.<br />

Activity interaction: Traditionally, before the email era, messages were<br />

sent via mailboxes (passive objects providing buffering) and distributed<br />

to the mailbox of the addressee. If the sender and receiver are close to<br />

each other (like threads in the same JVM), and the sender knows (has<br />

a reference to) where the receiver is, the overhead of distribution (cost<br />

of the stamp and mail delay) can be avoided by putting the message<br />

directly in the receivers mailbox. Anyhow, even if the messages (transmitted<br />

objects) are buffered in a passive object (the mailbox), the aim<br />

is to send it to some other active object (person), which deserves special<br />

support.<br />

Distribution: If the sender and the receiver of a message are not close to or<br />

not aware of each others location, it is desirable to have some mail distribution<br />

system. Even if the topic of distributed systems is outside the<br />

scope of this chapter, communicating via messages makes us prepared<br />

for the situation when each message has to be sent via ordinary mail; the<br />

message can look the same, only the mailing differs. If we would call the<br />

receiving active object directly, the addressee would need to be available<br />

at that particular time. Therefore, communicating via messages can be<br />

a good generic solution.<br />

Encapsulation: Data protection (using private or protected in Java) ensures<br />

encapsulation of sequential programs, which means that objects will be<br />

ensured to function independently of each other. For real-time programs<br />

that is not enough; when methods of another object is called from a<br />

time-aware thread (looking at the clock frequently), the caller “gives up<br />

it’s own will” and commits itself to performing the instructions given in<br />

the (hidden) implementation of the called method, which can result in<br />

blocking for a long time. Hence, threads give up their timely behavior<br />

unless communication is performed differently.<br />

85

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

Saved successfully!

Ooh no, something went wrong!