Example17.3

「Example17.3」の編集履歴(バックアップ)一覧はこちら

Example17.3 - (2010/10/24 (日) 10:52:28) の1つ前との変更点

追加された行は緑色になります。

削除された行は赤色になります。

**17.3 Futures **17.3 フューチャー A future is a value which is computed in parallel to some other client thread, to be used by the client thread at some future time. Futures are used in order to make good use of parallel processing resources. A typical usage is: フューチャーは、他のクライアントスレッドにおいて並列に計算される値であり、 クライアントスレッドによっていつか使用されます。フューチャーは並行プロセスのリソースを有効に利用するために使います。典型的な使い方は次のようです。 import scala.concurrent.ops._ ... val x = future(someLengthyComputation) anotherLengthyComputation val y = f(x()) + g(x()) The future method is defined in object scala.concurrent.ops as follows. future メソッドはオブジェクト scala.concurrent.ops において、次のように定義されています。 def future[A](p: => A): Unit => A = { val result = new SyncVar[A] fork { result.set(p) } (() => result.get) } The future method gets as parameter a computation p to be performed. The type of the computation is arbitrary; it is represented by future's type parameter a. The future method defines a guard result, which takes a parameter representing the result of the computation. It then forks off a new thread that computes the result and invokes the result guard when it is finished. In parallel to this thread, the function returns an anonymous function of type a. When called, this functions waits on the result guard to be invoked, and, once this happens returns the result argument. At the same time, the function reinvokes the result guard with the same argument,so that future invocations of the function can return the result immediately. futureメソッドは、実行すべき計算 p をパラメータにとります。計算の型は任意であり、そのことは future の型パラメータ A によって表現されています。future メソッドでは計算結果を表すパラメータを取る、ガード 'result' を定義します。つぎに、新しいスレッドを fork して、答えを計算し終了時に result ガードを起動します。このスレッドに並行して、関数(訳注:future)は型 A の無名関数を返します。この無名関数は、呼ばれたときに result ガードが起動されるまでウェイトし、そして、一度 result ガードが起動されると結果値を返します。同じ時に、関数は同じ引数で result ガードを再起動しますが、関数の future 起動は結果をすぐに返せます。 ---- #comment
#co(){ 17.3 Futures A future is a value which is computed in parallel to some other client thread, to be used by the client thread at some future time. Futures are used in order to make good use of parallel processing resources. A typical usage is:} ** 17.3 フューチャー &bold(){フューチャー}は、他のクライアントスレッドにおいて並列に計算される値であり、クライアントスレッドによっていつか使用されます。フューチャーは並行プロセスのリソースを有効に利用するために使います。典型的な使い方は次のようです。 import scala.concurrent.ops._ ... val x = future(someLengthyComputation) anotherLengthyComputation val y = f(x()) + g(x()) #co(){ The future method is defined in object scala.concurrent.ops as follows. } future メソッドはオブジェクト scala.concurrent.ops において、次のように定義されています。 def future[A](p: => A): Unit => A = { val result = new SyncVar[A] fork { result.set(p) } (() => result.get) } #co(){ The future method gets as parameter a computation p to be performed. The type of the computation is arbitrary; it is represented by future's type parameter a. The future method defines a guard result, which takes a parameter representing the result of the computation. It then forks off a new thread that computes the result and invokes the result guard when it is finished. In parallel to this thread, the function returns an anonymous function of type a. When called, this functions waits on the result guard to be invoked, and, once this happens returns the result argument. At the same time, the function reinvokes the result guard with the same argument,so that future invocations of the function can return the result immediately. } futureメソッドは、実行すべき計算 p を引数にとります。計算の型は任意であり、そのことは future の型パラメータ A によって表現されています。future メソッドでは計算結果を表す引数を取る、ガード result を定義します。つぎに、新しいスレッドを fork して、答えを計算し終了時に result ガードを起動します。このスレッドに並行して、関数 (訳注 : future) は型 A の無名関数を返します。この無名関数は、呼ばれたときに result ガードが起動されるまでウェイトし、そして、一度 result ガードが起動されると結果値を返します。同じ時に、関数は同じ引数で result ガードを再起動しますが、関数の future 起動は結果をすぐに返せます。 #center(){[[前ページ>Example17.2]] [[ 17 章>Chapter 17 Abstractions for Concurrency]] [[目次>ScalaByExample和訳]] [[次ページ>Example17.4]]} ---- #comment

表示オプション

横に並べて表示:
変化行の前後のみ表示:
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。