
1
MultithreadedInputStream
CMSC 433, project 3
Purpose
• InputStream wrapper
• Used when reading stream is
computationally expensive
• might want to do it in background
Design
• InputStream wrapper
• Background thread reads from input
– stores in buffer(s)
• read calls read from buffer(s) if filled
– otherwise, reads directly from input
in
background
thread
Buffer Queue
read requests
either/or
only if
current buffer
and queue is
empty
current
buffer
Dataflow
read function
• If current buffer empty
– Try to dequeue buffer from buffer queue
– make result new current buffer
• Does current buffer contain data?
– if so, fulfill request out of current buffer
• else, try to read data directly
Notes:
• read should not hold lock for entire
operation
– don’t hold lock while copying data