1MultithreadedInputStreamCMSC 433, project 3Purpose• InputStream wrapper• Used when reading stream iscomputationally expensive• might want to do it in backgroundDesign• InputStream wrapper• Background thread reads from input– stores in buffer(s)• read calls read from buffer(s) if filled– otherwise, reads directly from inputinbackgroundthreadBuffer Queueread requestseither/oronly ifcurrent buffer and queue is emptycurrentbufferDataflowread 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 directlyNotes:• read should not hold lock for entireoperation– don’t hold lock while copying data2background 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
View Full Document