|
Class: CollectingSharedQueueStream
Object
|
+--Collection
|
+--SequenceableCollection
|
+--CollectingSharedQueueStream
- Package:
- stx:libbasic2
- Category:
- Streams
- Version:
- rev:
1.5
date: 2017/11/28 18:23:55
- user: cg
- file: CollectingSharedQueueStream.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Claus Gittinger
This class provides a buffering mechanism between a reader and a writer
process (i.e. it is much like a sharedQueue), but remembers the data as
written by the writer internally, providing indexed access to elements.
The reader side may read from it using #next, and possibly access
elements via #at:.
Reading/accessing may start immediately, but will block until enough elements
have been added by another process, the writer.
Instances of this class may be useful to start processing on
big document/data collection immediately, while the data is still being
read by another thread;
A concrete application is the HTMLDocumentReader, which is being changed to
start processing and displaying the document while the rest is still being read.
Stream
OrderedCollection
SharedQueue
instance creation
-
new
-
(comment from inherited method)
return an instance of myself without indexed variables
accessing
-
at: index
-
synchronized read - possibly wait for elements up to index
being added (by someone else); then return it.
-
next
-
return the next value in the queue; if there is none,
wait 'til something is put into the receiver.
-
nextPut: anObject
-
append anObject to the queue; if anyone is waiting, tell him
accessing-special
-
close
-
signal the end of input; to be used by the writer
-
finalSize: aNumber
-
can be used by the writer, if the final size is known in
advance.
-
signalChanges: aBoolean
-
controls if I should send out size-changeMessages when new elements arrive
dummy converting
-
asStringCollection
-
(comment from inherited method)
return a new string collection containing the elements;
these ought to be strings. (i.e. String or Text instances)
initialization
-
initialize
-
(comment from inherited method)
just to ignore initialize to objects which do not need it
queries
-
atEnd
-
-
currentSize
-
-
size
-
must wait until closed
two processes synchronized much like with a sharedQueue:
|s reader|
s := CollectingSharedQueueStream new.
reader := [
[s atEnd] whileFalse:[
Transcript showCR:s next
].
] fork.
1 to:10 do:[:i |
Delay waitForSeconds:1.
s nextPut:i
].
|
the writer reads from a (slow) pipe;
the reader sends it to the transcript.
|pipe s reader|
s := CollectingSharedQueueStream new.
reader := [
[s atEnd] whileFalse:[
Transcript showCR:s next
].
] forkAt:9.
pipe := PipeStream readingFrom:'ls -lR /usr'.
pipe notNil ifTrue:[
[pipe atEnd] whileFalse:[
pipe readWait.
s nextPut:(pipe nextLine).
].
pipe close.
].
s close
|
the writer reads from a (slow) pipe;
the collection is used in a TextView, which
will block whenever lines are to be displayed, which have not
yet been read:
|view pipe buffer reader|
buffer := CollectingSharedQueueStream new.
buffer finalSize:100.
[
pipe := PipeStream readingFrom:'ls -lR /usr'.
pipe notNil ifTrue:[
[pipe atEnd] whileFalse:[
pipe readWait.
buffer nextPut:(pipe nextLine).
].
pipe close.
].
buffer changed:#size.
buffer close.
] fork.
view := ScrollableView for:TextView.
view model:buffer; listMessage:#value; aspectMessage:#value.
view open.
|
|