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. Multi-Threaded Programming<br />

[5]<br />

[6]<br />

[4]<br />

[7]<br />

[3]<br />

[2]<br />

[1]<br />

RTEvent[] buffer<br />

currSize==7<br />

postAt<br />

[8]<br />

fetchAt<br />

[9]<br />

growth<br />

Figure 3.10: Ring buffer as an array with size 12, currently filled with 7 objects<br />

(referenced from the gray elements).<br />

tor, if the operation of the buffer needs to be synchronized with some<br />

other objects (such as an other buffer).<br />

The actual buffer/array is, as shown in Figure 3-10, declared as<br />

protected RTEvent[] buffer;<br />

Construction A new buffer is created by the following constructors, where<br />

the default maxSize is some system dependent DEFAULT_SIZE (currently<br />

64 in our system), and the default lock object is allocated and internally.<br />

The parameters maxSize and lock overrides the defaults:<br />

[10]<br />

[0]<br />

[11]<br />

public RTEventBuffer()<br />

public RTEventBuffer(int maxSize)<br />

public RTEventBuffer(int maxSize , Object lock)<br />

Posting The methods for posting events to the buffer are named doPost,<br />

tryPost, or simply post. The doPost method simply blocks the caller<br />

until the posting is completed. The tryPost gives up if the buffer is full,<br />

either immediately if no timeout is given or after the specified timeout (in<br />

ms). The method post is the generic one that is subject to replacement<br />

in subclasses, whereas the other methods are declared final for reasons<br />

of efficiency.<br />

The post method by default simply calls doPost and returns null. The<br />

return (of type RTEvent) value is useful when subclassing and changing<br />

post to call one of the tryPost methods, which return null when the<br />

event was successfully posted. The event supplied as an argument to<br />

tryPost is returned if not posted (due to the buffer being full). Returning<br />

the unposted event simplifies the calling code. The public posting<br />

methods are:<br />

92 2012-08-29 16:05

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

Saved successfully!

Ooh no, something went wrong!