Docsity

Multithreaded Input Stream - Lecture Slides | CMSC 433, Study Guides, Projects, Research of Programming Languages

Toggle sidebar
bg1
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
pf2

Partial preview of the text

Download Multithreaded Input Stream - Lecture Slides | CMSC 433 and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

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

currentbuffer

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

background thread

  • loop
    • get lock
    • read from in into new buffer
    • put buffer into buffer queue
    • release lock
    • if buffer queue had been empty
      • sleep for 10 milliseconds
      • allow foreground thread to read directly