eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'Future':

Home

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

Class: Future


Inheritance:

   ProtoObject
   |
   +--DelayedValue
      |
      +--Future

Package:
stx:libbasic2
Category:
Kernel-Processes
Version:
rev: 1.28 date: 2019/06/25 08:41:23
user: cg
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 whoever 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 whoever 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 whoever 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 whoever 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 whoever 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 whoever 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 whoever 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 whoever 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 whoever 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 whoever called me,
but ensure that any messages sent to me before execution
of the block has terminated are suspended until it has terminated.

initialization
o  initializeSemaphore
here, the semaphore is created when the evaluation block is defined

private
o  signalSemaphoreAfterForked: aBlock
common code for all block:* methods.
Execute aBlock in parallel with whatever called me,
and ensure that my private semaphore is signalled at the end.

o  signalSemaphoreAfterForked: aBlock atPriority: prio
common code for all block:* methods.
Execute aBlock in parallel with whatever called me,
and ensure that my private semaphore is signalled at the end.


Examples:


Starts evaluating the factorial immediately, but waits until the result is available before printing the answer
  | fac |

  fac := [10000 factorial printString] 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.. '. 100000 factorial. Transcript showCR: 'Finished fac1'] futureValue.
  fac2 := [Transcript showCR: 'Starting fac2.. '. 150000 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 t1 t2 tAll|

      tAll := TimeDuration toRun:[
          t1 := TimeDuration toRun:[
              p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib /etc'.
              text := p contents.
              p close.
          ].
          t2 := TimeDuration toRun:[
              v := TextView new openAndWait.
          ].    
          v contents:text
      ].
      Transcript showCR:'Time to read: %1' with:t1.
      Transcript showCR:'Time to open: %1' with:t2.
      Transcript showCR:'Time overall: %1' with:tAll.
The same here:
      |p text v|

      v := TextView new openAndWait.
      p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib /etc'.
      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 t1 t2 tAll|

      tAll := TimeDuration toRun:[
          text := [   
                      |p t|

                      t1 := TimeDuration toRun:[
                          p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib /etc'.
                          t := p contents.
                          p close.
                      ].     
                      t
                  ] futureValue.
          t2 := TimeDuration toRun:[
              v := TextView new openAndWait.
          ].    
          v contents:text
      ].
      Transcript showCR:'Time to read: %1' with:t1.
      Transcript showCR:'Time to open: %1' with:t2.
      Transcript showCR:'Time overall: %1' with:tAll.


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Fri, 19 Apr 2024 00:20:44 GMT