|
Class: Future
ProtoObject
|
+--Future
- Package:
- stx:libbasic2
- Category:
- Kernel-Processes
- Version:
- rev:
1.24
date: 2018/02/01 22:44:21
- user: stefan
- file: Future.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- tph@cs.man.ac.uk
I represent an execution in progress, which will be required some time
in the future.
I will immediately start execution in a separate process,
but delay any messages sent to me, until the execution has completed.
This is useful for time consuming operations (print jobs, compile jobs etc.),
which can be done in the background and the user can do something else
in the meantime. If the computation is finished before the user needs its
value, he is not forced to wait.
If the computation is unfinished, he has to wait for the remaining time only.
Block
Lazy
LazyValue
evaluating
-
block: aBlock
-
Execute aBlock in parallel with whatever called me, but
ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.
-
block: aBlock value: aValue
-
Execute aBlock in parallel with whatever called me, but
ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.
-
block: aBlock value: value1 value: value2
-
Execute aBlock in parallel with whatever called me, but
ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.
-
block: aBlock value: value1 value: value2 value: value3
-
Execute aBlock in parallel with whatever called me, but
ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.
-
block: aBlock valueWithArguments: anArray
-
Execute aBlock in parallel with whatever called me, but
ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.
-
priority: prio block: aBlock
-
Execute aBlock in parallel with whatever called me, but
ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.
-
priority: prio block: aBlock value: aValue
-
Execute aBlock in parallel with whatever called me, but
ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.
-
priority: prio block: aBlock value: value1 value: value2
-
Execute aBlock in parallel with whatever called me, but
ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.
-
priority: prio block: aBlock value: value1 value: value2 value: value3
-
Execute aBlock in parallel with whatever called me, but
ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.
-
priority: prio block: aBlock valueWithArguments: anArray
-
Execute aBlock in parallel with whatever called me, but
ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.
printing
-
displayOn: aGCOrStream
-
what a kludge - Dolphin and Squeak mean: printOn: a stream;
-
displayString
-
attention: TextStream is not present in ultra-mini standalone apps
synchronising
-
doesNotUnderstand: aMessage
-
Any message to a Future will end up here.
-
perform: aSelector withArguments: argArray
-
send the message aSelector with all args taken from argArray
to the receiver.
-
value
-
Wait for evaluation to complete
testing
-
hasValue
-
-
isLazyValue
-
Starts evaluating the factorial immediately, but waits until
the result is available before printing the answer
| fac |
fac := [50000 factorial] futureValue.
Transcript showCR: 'evaluating factorial...'.
Dialog information:'You can do something useful now...'.
Transcript showCR: fac
|
An example illustrating the use of multiple futures and
explicit resynchronisation.
Starts evaluating both factorials immediately, but waits until
both blocks have finished before continuing.
| fac1 fac2 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 9000 factorial. Transcript showCR: 'Finished fac1'] futureValue.
fac2 := [Transcript showCR: 'Starting fac2.. '. 5000 factorial. Transcript showCR: 'Finished fac2'] futureValue.
fac2 value.
fac1 value.
Transcript showCR: 'both completed.'.
|
Example showing how arguments may be passed to futures.
| temp |
temp := [:x :y | 10 * x * y] futureValue: 3 value: 4.
Transcript showCR: temp.
|
Claus:
The above examples do not really show the power of Futures;
they can be useful, whenever some long-time computation is
to be done, and some other useful work can be done in the meanwhile.
for example:
Without futures, the inputfile is read before opening the view;
the readTime and view creation times sum up:
|p text v|
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
text := p contents.
p close.
v := TextView new openAndWait.
v contents:text
|
The same here:
|p text v|
v := TextView new openAndWait.
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
text := p contents.
p close.
v contents:text
|
With futures, the view creation and reading are done in parallel:
(if the windowing system is slow when opening the view, the contents may
be already available - especially on X window systems, where the user
has to provide the window position with the mouse)
|p text v|
text := [ |p t|
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
t := p contents.
p close.
t
] futureValue.
v := TextView new openAndWait.
v contents:text
|
|