author | Claus Gittinger <cg@exept.de> |
Thu, 08 Nov 2007 17:34:59 +0100 | |
changeset 1913 | 4c2622e92a91 |
parent 1516 | 08829dfb29fe |
child 2341 | 36e455705ee6 |
permissions | -rw-r--r-- |
1140 | 1 |
" |
2 |
This is a Manchester Goodie protected by copyright. |
|
3 |
These conditions are imposed on the whole Goodie, and on any significant |
|
4 |
part of it which is separately transmitted or stored: |
|
5 |
* You must ensure that every copy includes this notice, and that |
|
6 |
source and author(s) of the material are acknowledged. |
|
7 |
* These conditions must be imposed on anyone who receives a copy. |
|
8 |
* The material shall not be used for commercial gain without the prior |
|
9 |
written consent of the author(s). |
|
10 |
Further information on the copyright conditions may be obtained by |
|
11 |
sending electronic mail: |
|
12 |
To: goodies-lib@cs.man.ac.uk |
|
13 |
Subject: copyright |
|
14 |
or by writing to The Smalltalk Goodies Library Manager, Dept of |
|
15 |
Computer Science, The University, Manchester M13 9PL, UK |
|
16 |
||
17 |
(C) Copyright 1992 University of Manchester |
|
18 |
For more information about the Manchester Goodies Library (from which |
|
19 |
this file was distributed) send e-mail: |
|
20 |
To: goodies-lib@cs.man.ac.uk |
|
21 |
Subject: help |
|
22 |
" |
|
23 |
||
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
484
diff
changeset
|
24 |
"{ Package: 'stx:goodies' }" |
457 | 25 |
|
1327 | 26 |
ProtoObject subclass:#Future |
1140 | 27 |
instanceVariableNames:'result semaphore' |
28 |
classVariableNames:'' |
|
29 |
poolDictionaries:'' |
|
30 |
category:'Kernel-Processes' |
|
62 | 31 |
! |
32 |
||
484 | 33 |
!Future class methodsFor:'documentation'! |
34 |
||
35 |
copyright |
|
36 |
" |
|
37 |
This is a Manchester Goodie protected by copyright. |
|
38 |
These conditions are imposed on the whole Goodie, and on any significant |
|
39 |
part of it which is separately transmitted or stored: |
|
40 |
* You must ensure that every copy includes this notice, and that |
|
41 |
source and author(s) of the material are acknowledged. |
|
42 |
* These conditions must be imposed on anyone who receives a copy. |
|
43 |
* The material shall not be used for commercial gain without the prior |
|
44 |
written consent of the author(s). |
|
45 |
Further information on the copyright conditions may be obtained by |
|
46 |
sending electronic mail: |
|
47 |
To: goodies-lib@cs.man.ac.uk |
|
48 |
Subject: copyright |
|
49 |
or by writing to The Smalltalk Goodies Library Manager, Dept of |
|
50 |
Computer Science, The University, Manchester M13 9PL, UK |
|
51 |
||
52 |
(C) Copyright 1992 University of Manchester |
|
53 |
For more information about the Manchester Goodies Library (from which |
|
54 |
this file was distributed) send e-mail: |
|
55 |
To: goodies-lib@cs.man.ac.uk |
|
56 |
Subject: help |
|
57 |
" |
|
58 |
! |
|
59 |
||
60 |
documentation |
|
61 |
" |
|
62 |
I represent an execution in progress. |
|
1327 | 63 |
I will immediately start execution in a separate process. |
484 | 64 |
Any messages sent to me are delayed until execution has completed.' |
1327 | 65 |
|
66 |
[author:] |
|
67 |
tph@cs.man.ac.uk |
|
1516 | 68 |
|
69 |
[see also:] |
|
70 |
Block Lazy LazyValue |
|
484 | 71 |
" |
72 |
! |
|
73 |
||
74 |
examples |
|
75 |
" |
|
76 |
Starts evaluating the factorial immediately, but waits until |
|
77 |
the result is available before printing the answer |
|
1516 | 78 |
[exBegin] |
484 | 79 |
| fac | |
80 |
||
1516 | 81 |
fac := [5000 factorial] futureValue. |
484 | 82 |
Transcript showCR: 'evaluating factorial...'. |
83 |
Transcript showCR: fac printString |
|
1516 | 84 |
[exEnd] |
484 | 85 |
|
86 |
||
87 |
An example illustrating the use of multiple futures and |
|
88 |
explicit resynchronisation. |
|
89 |
||
90 |
Starts evaluating both factorials immediately, but waits until |
|
91 |
both blocks have finished before continuing. |
|
1516 | 92 |
[exBegin] |
484 | 93 |
| fac1 fac2 | |
94 |
||
95 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 1000 factorial] futureValue. |
|
96 |
fac2 := [Transcript showCR: 'Starting fac2.. '. 2000 factorial] futureValue. |
|
1516 | 97 |
fac2 isString. |
98 |
fac1 isString. |
|
484 | 99 |
Transcript showCR: 'both completed.'. |
1516 | 100 |
[exEnd] |
484 | 101 |
|
102 |
Example showing how arguments may be passed to futures. |
|
1516 | 103 |
[exBegin] |
484 | 104 |
| temp | |
105 |
||
106 |
temp := [:x :y | 10 * x * y] futureValue: 3 value: 4. |
|
107 |
Transcript showCR: temp printString. |
|
108 |
||
1516 | 109 |
[exEnd] |
484 | 110 |
|
111 |
Claus: |
|
112 |
The above examples do not really show the power of Futures; |
|
113 |
they can be useful, whenever some long-time computation is |
|
114 |
to be done, and some other useful work can be done in the meanwhile. |
|
115 |
for example: |
|
116 |
||
117 |
Here, the input is read before - the readTime and view creation |
|
118 |
times sum up: |
|
1516 | 119 |
[exBegin] |
120 |
|p text v| |
|
484 | 121 |
|
1516 | 122 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'. |
123 |
text := p contents. |
|
124 |
p close. |
|
125 |
v := TextView new openAndWait. |
|
126 |
v contents:text |
|
127 |
[exEnd] |
|
484 | 128 |
|
129 |
The same here: |
|
1516 | 130 |
[exBegin] |
131 |
|p text v| |
|
484 | 132 |
|
1516 | 133 |
v := TextView new openAndWait. |
134 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'. |
|
135 |
text := p contents. |
|
136 |
p close. |
|
137 |
v contents:text |
|
138 |
[exEnd] |
|
484 | 139 |
|
140 |
||
141 |
Here, the view creation and reading are done in parallel: |
|
142 |
(if the user is slow when opening the view, the contents may |
|
143 |
be already available) |
|
1516 | 144 |
[exBegin] |
145 |
|p text v| |
|
484 | 146 |
|
1516 | 147 |
text := [ |p t| |
484 | 148 |
|
1516 | 149 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'. |
150 |
t := p contents. |
|
151 |
p close. |
|
152 |
t |
|
153 |
] futureValue. |
|
154 |
v := TextView new openAndWait. |
|
155 |
v contents:text |
|
156 |
[exEnd] |
|
484 | 157 |
" |
158 |
! ! |
|
159 |
||
1140 | 160 |
!Future methodsFor:'evaluating'! |
62 | 161 |
|
162 |
block: aBlock |
|
1514 | 163 |
"Execute aBlock in parallel with whatever called me, but |
164 |
ensure that any messages sent to me before execution |
|
165 |
of the block has terminated are suspended until it has terminated." |
|
62 | 166 |
|
1514 | 167 |
semaphore := Semaphore new name:'Future'. |
168 |
[result := aBlock ensure:[semaphore signal]] fork |
|
62 | 169 |
! |
170 |
||
171 |
block: aBlock value: aValue |
|
1514 | 172 |
"Execute aBlock in parallel with whatever called me, but |
173 |
ensure that any messages sent to me before execution |
|
174 |
of the block has terminated are suspended until it has terminated." |
|
62 | 175 |
|
1514 | 176 |
semaphore := Semaphore new name:'Future'. |
177 |
[ |
|
178 |
result := [ |
|
179 |
aBlock value:aValue |
|
180 |
] ensure:[semaphore signal] |
|
181 |
] fork |
|
62 | 182 |
! |
183 |
||
184 |
block: aBlock value: value1 value: value2 |
|
1514 | 185 |
"Execute aBlock in parallel with whatever called me, but |
186 |
ensure that any messages sent to me before execution |
|
187 |
of the block has terminated are suspended until it has terminated." |
|
62 | 188 |
|
1514 | 189 |
semaphore := Semaphore new name:'Future'. |
190 |
[ |
|
191 |
result := [ |
|
192 |
aBlock value: value1 value: value2 |
|
193 |
] ensure:[semaphore signal] |
|
194 |
] fork |
|
62 | 195 |
! |
196 |
||
197 |
block: aBlock value: value1 value: value2 value: value3 |
|
1514 | 198 |
"Execute aBlock in parallel with whatever called me, but |
199 |
ensure that any messages sent to me before execution |
|
200 |
of the block has terminated are suspended until it has terminated." |
|
62 | 201 |
|
1514 | 202 |
semaphore := Semaphore new name:'Future'. |
203 |
[ |
|
204 |
result := [ |
|
205 |
aBlock value: value1 value: value2 value: value3 |
|
206 |
] ensure:[semaphore signal] |
|
207 |
] fork |
|
62 | 208 |
! |
209 |
||
210 |
block: aBlock valueWithArguments: anArray |
|
1514 | 211 |
"Execute aBlock in parallel with whatever called me, but |
212 |
ensure that any messages sent to me before execution |
|
213 |
of the block has terminated are suspended until it has terminated." |
|
62 | 214 |
|
1514 | 215 |
semaphore := Semaphore new name:'Future'. |
216 |
[ |
|
217 |
result := [ |
|
218 |
aBlock valueWithArguments: anArray |
|
219 |
] ensure:[semaphore signal] |
|
220 |
] fork |
|
62 | 221 |
! ! |
222 |
||
1140 | 223 |
!Future methodsFor:'synchronising'! |
224 |
||
1514 | 225 |
doesNotUnderstand:aMessage |
226 |
"Any message to a Future will end up here." |
|
227 |
||
228 |
semaphore waitUncounted. "Wait for evaluation to complete" |
|
229 |
"(if not already completed)" |
|
230 |
^ result perform:aMessage selector withArguments:aMessage arguments |
|
1140 | 231 |
! ! |
62 | 232 |
|
1327 | 233 |
!Future methodsFor:'testing'! |
234 |
||
1514 | 235 |
hasValue |
236 |
^ semaphore wouldBlock not |
|
237 |
! |
|
238 |
||
1327 | 239 |
isLazyValue |
240 |
^ semaphore wouldBlock |
|
241 |
! ! |
|
242 |
||
1140 | 243 |
!Future class methodsFor:'documentation'! |
244 |
||
245 |
version |
|
1516 | 246 |
^ '$Header: /cvs/stx/stx/libbasic2/Future.st,v 1.10 2005-01-26 13:55:27 stefan Exp $' |
62 | 247 |
! ! |