eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'Future':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: Future


Inheritance:

   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

Description:


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.


Related information:

    Block
    Lazy
    LazyValue

Instance protocol:

evaluating
o  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.

o  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.

o  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.

o  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.

o  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.

o  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.

o  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.

o  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.

o  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.

o  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
o  displayOn: aGCOrStream
what a kludge - Dolphin and Squeak mean: printOn: a stream;

o  displayString
attention: TextStream is not present in ultra-mini standalone apps

synchronising
o  doesNotUnderstand: aMessage
Any message to a Future will end up here.

o  perform: aSelector withArguments: argArray
send the message aSelector with all args taken from argArray
to the receiver.

o  value
Wait for evaluation to complete

testing
o  hasValue

o  isLazyValue


Examples:


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


ST/X 7.1.0.0; WebServer 1.663 at exept.de:8081; Mon, 04 Aug 2025 16:47:12 GMT