Future.st
changeset 1516 08829dfb29fe
parent 1514 83af7e61d415
child 2341 36e455705ee6
equal deleted inserted replaced
1515:006cd21bbc25 1516:08829dfb29fe
    63     I will immediately start execution in a separate process.
    63     I will immediately start execution in a separate process.
    64     Any messages sent to me are delayed until execution has completed.'
    64     Any messages sent to me are delayed until execution has completed.'
    65 
    65 
    66     [author:]        
    66     [author:]        
    67         tph@cs.man.ac.uk
    67         tph@cs.man.ac.uk
       
    68 
       
    69     [see also:]
       
    70         Block Lazy LazyValue
    68 "
    71 "
    69 !
    72 !
    70 
    73 
    71 examples
    74 examples
    72 "
    75 "
    73   Starts evaluating the factorial immediately, but waits until
    76   Starts evaluating the factorial immediately, but waits until
    74   the result is available before printing the answer
    77   the result is available before printing the answer
    75 								    [exBegin]
    78                                                                     [exBegin]
    76     | fac |
    79     | fac |
    77 
    80 
    78     fac := [100 factorial] futureValue.
    81     fac := [5000 factorial] futureValue.
    79     Transcript showCR: 'evaluating factorial...'.
    82     Transcript showCR: 'evaluating factorial...'.
    80     Transcript showCR: fac printString
    83     Transcript showCR: fac printString
    81 								    [exEnd]
    84                                                                     [exEnd]
    82 
    85 
    83 
    86 
    84   An example illustrating the use of multiple futures and
    87   An example illustrating the use of multiple futures and
    85   explicit resynchronisation.
    88   explicit resynchronisation.
    86 
    89 
    87   Starts evaluating both factorials immediately, but waits until
    90   Starts evaluating both factorials immediately, but waits until
    88   both blocks have finished before continuing.
    91   both blocks have finished before continuing.
    89 								    [exBegin]
    92                                                                     [exBegin]
    90     | fac1 fac2 |
    93     | fac1 fac2 |
    91 
    94 
    92     fac1 := [Transcript showCR: 'Starting fac1.. '. 1000 factorial] futureValue.
    95     fac1 := [Transcript showCR: 'Starting fac1.. '. 1000 factorial] futureValue.
    93     fac2 := [Transcript showCR: 'Starting fac2.. '. 2000 factorial] futureValue.
    96     fac2 := [Transcript showCR: 'Starting fac2.. '. 2000 factorial] futureValue.
    94     fac2 touch.
    97     fac2 isString.
    95     fac1 touch.
    98     fac1 isString.
    96     Transcript showCR: 'both completed.'.
    99     Transcript showCR: 'both completed.'.
    97 								    [exEnd]
   100                                                                     [exEnd]
    98 
   101 
    99   Example showing how arguments may be passed to futures.
   102   Example showing how arguments may be passed to futures.
   100 								    [exBegin]
   103                                                                     [exBegin]
   101     | temp |
   104     | temp |
   102 
   105 
   103     temp := [:x :y | 10 * x * y] futureValue: 3 value: 4.
   106     temp := [:x :y | 10 * x * y] futureValue: 3 value: 4.
   104     Transcript  showCR: temp printString.
   107     Transcript  showCR: temp printString.
   105 
   108 
   106 								    [exEnd]
   109                                                                     [exEnd]
   107 
   110 
   108   Claus:
   111   Claus:
   109     The above examples do not really show the power of Futures;
   112     The above examples do not really show the power of Futures;
   110     they can be useful, whenever some long-time computation is
   113     they can be useful, whenever some long-time computation is
   111     to be done, and some other useful work can be done in the meanwhile.
   114     to be done, and some other useful work can be done in the meanwhile.
   112     for example:
   115     for example:
   113 
   116 
   114     Here, the input is read before - the readTime and view creation
   117     Here, the input is read before - the readTime and view creation
   115     times sum up:
   118     times sum up:
   116 								    [exBegin]
   119                                                                     [exBegin]
   117 	|p text v|
   120         |p text v|
   118 
   121 
   119 	p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
   122         p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
   120 	text := p contents.
   123         text := p contents.
   121 	p close.
   124         p close.
   122 	v := TextView new openAndWait.
   125         v := TextView new openAndWait.
   123 	v contents:text
   126         v contents:text
   124 								    [exEnd]
   127                                                                     [exEnd]
   125 
   128 
   126     The same here:
   129     The same here:
   127 								    [exBegin]
   130                                                                     [exBegin]
   128 	|p text v|
   131         |p text v|
   129 
   132 
   130 	v := TextView new openAndWait.
   133         v := TextView new openAndWait.
   131 	p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
   134         p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
   132 	text := p contents.
   135         text := p contents.
   133 	p close.
   136         p close.
   134 	v contents:text
   137         v contents:text
   135 								    [exEnd]
   138                                                                     [exEnd]
   136 
   139 
   137 
   140 
   138     Here, the view creation and reading are done in parallel:
   141     Here, the view creation and reading are done in parallel:
   139     (if the user is slow when opening the view, the contents may
   142     (if the user is slow when opening the view, the contents may
   140      be already available)
   143      be already available)
   141 								    [exBegin]
   144                                                                     [exBegin]
   142 	|p text v|
   145         |p text v|
   143 
   146 
   144 	text := [   |p t|
   147         text := [   |p t|
   145 
   148 
   146 		    p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
   149                     p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
   147 		    t := p contents.
   150                     t := p contents.
   148 		    p close.
   151                     p close.
   149 		    t
   152                     t
   150 		] futureValue.
   153                 ] futureValue.
   151 	v := TextView new openAndWait.
   154         v := TextView new openAndWait.
   152 	v contents:text
   155         v contents:text
   153 								    [exEnd]
   156                                                                     [exEnd]
   154 "
   157 "
   155 ! !
   158 ! !
   156 
   159 
   157 !Future methodsFor:'evaluating'!
   160 !Future methodsFor:'evaluating'!
   158 
   161 
   238 ! !
   241 ! !
   239 
   242 
   240 !Future class methodsFor:'documentation'!
   243 !Future class methodsFor:'documentation'!
   241 
   244 
   242 version
   245 version
   243     ^ '$Header: /cvs/stx/stx/libbasic2/Future.st,v 1.9 2005-01-26 13:41:37 stefan Exp $'
   246     ^ '$Header: /cvs/stx/stx/libbasic2/Future.st,v 1.10 2005-01-26 13:55:27 stefan Exp $'
   244 ! !
   247 ! !