Example17.1

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

Example17.1 - (2010/10/27 (水) 13:14:01) の最新版との変更点

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

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

#co(){ 17.1 Signals and Monitors Example 17.1.1 The monitor provides the basic means for mutual exclusion of processes in Scala. Every instance of class AnyRef can be used as a monitor by calling one or more of the methods below. } ** 17.1 シグナルとモニター (Signals and Monitors) &b(){Example 17.1.1 } &bold(){モニター}は Scala におけるプロセスの相互排他処理の基本的な手段を提供します。 AnyRef クラスの各インスタンスは、次の1つあるいは複数のメソッドを呼ぶことでモニターとして使えます。 def synchronized[A] (e: => A): A def wait() def wait(msec: Long) def notify() def notifyAll() #co(){ The synchronized method executes its argument computation e in mutual exclusive mode - at any one time, only one thread can execute a synchronized argument of a given monitor. } 同期メソッドは相互排他的に、つまり同時にはただ1つのスレッドだけが与えられたモニターの同期化引数を実行できる方式で、その引数である計算 e を実行します。 #co(){ Threads can suspend inside a monitor by waiting on a signal. Threads that call the wait method wait until a notify method of the same object is called subsequently by some other thread. Calls to notify with no threads waiting for the signal are ignored. } スレッドはシグナルをウェイトすることで、モニター内でサスペンドできます。 wait メソッドを呼び出すスレッドは、同じオブジェクトの notify メソッドが他のスレッドからその後呼ばれるまで、ウェイトします。 #co(){ There is also a timed form of wait, which blocks only as long as no signal was received or the specified amount of time (given in milliseconds) has elapsed. Furthermore, there is a notifyAll method which unblocks all threads which wait for the signal. These methods, as well as class Monitor are primitive in Scala; they are implemented in terms of the underlying runtime system. } ウェイトの制限時間つき形態もあり、シグナルを受信するか、あるいは指定された時間(ミリ秒で与えられる)が過ぎるまでブロックします。さらにまた、シグナルを待つ全てのスレッドをアンブロックする notifyAll メソッドもあります。これらのメソッドは Monitor クラスと同様に、Scala ではプリミティブです。つまり、それらは実行時システムによって実装されています。 #co(){ Typically, a thread waits for some condition to be established. If the condition does not hold at the time of the wait call, the thread blocks until some other thread has established the condition. It is the responsibility of this other thread to wake up waiting processes by issuing a notify or notifyAll. Note however, that there is no guarantee that a waiting process gets to run immediately after the call to notify is issued. It could be that other processes get to run first which invalidate the condition again. Therefore, the correct form of waiting for a condition C uses a while loop: } 典型的には、スレッドはある条件が確立するまでウェイトします。もしその条件が wait をコールするまでに確立していなければ、そのスレッドは、他のスレッドがその条件を確立するまで、ブロックします。notify あるいは notifyAll を発行してウェイトしているプロセスを起動するのは、他のスレッドの責任です。しかし、ウェイトしているプロセスが notify 発行後すぐに実行されるという保証はありません。他のプロセスが最初に実行され、その条件を無効にすることもあり得ます。ですから、条件 C の確立をウェイトする正しい形は、while ループを使うことです。 while (!C ) wait() #co(){ As an example of how monitors are used, here is is an implementation of a bounded buffer class. } モニターの使用例として、境界付きバッファの実装をあげておきます。 class BoundedBuffer[A](N: Int) { var in = 0, out = 0, n = 0 val elems = new Array[A](N) def put(x: A) = synchronized { while (n >= N) wait() elems(in) = x ; in = (in + 1) % N ; n = n + 1 if (n == 1) notifyAll() } def get: A = synchronized { while (n == 0) wait() val x = elems(out) ; out = (out + 1) % N ; n = n - 1 if (n == N - 1) notifyAll() x } } #co(){ And here is a program using a bounded buffer to communicate between a producer and a consumer process. } 境界付きバッファを使って生産プロセスと消費プロセスとの間でコミュニケーションをとるプログラムを見てみましょう。 import scala.concurrent.ops._ ... val buf = new BoundedBuffer[String](10) spawn { while (true) { val s = produceString ; buf.put(s) } } spawn { while (true) { val s = buf.get ; consumeString(s) } } } #co(){ The spawn method spawns a new thread which executes the expression given in the parameter. It is defined in object concurrent.ops as follows. } spawn メソッドは、引数で与えられた式を実行する新しいスレッドを生成します。それはオブジェクト scala.concurrent.ops において、次のように定義されています。 def spawn(p: => Unit) { val t = new Thread() { override def run() = p } t.start() } #center(){[[前ページ>Chapter 17 Abstractions for Concurrency]] [[ 17 章>Chapter 17 Abstractions for Concurrency]] [[目次>ScalaByExample和訳]] [[次ページ>Example17.2]]} ---- #comment
#co(){ 17.1 Signals and Monitors Example 17.1.1 The monitor provides the basic means for mutual exclusion of processes in Scala. Every instance of class AnyRef can be used as a monitor by calling one or more of the methods below. } #setmenu2(ex-r-menu) ** 17.1 シグナルとモニター (Signals and Monitors) &b(){Example 17.1.1 } &bold(){モニター}は Scala におけるプロセスの相互排他処理の基本的な手段を提供します。 AnyRef クラスの各インスタンスは、次の1つあるいは複数のメソッドを呼ぶことでモニターとして使えます。 def synchronized[A] (e: => A): A def wait() def wait(msec: Long) def notify() def notifyAll() #co(){ The synchronized method executes its argument computation e in mutual exclusive mode - at any one time, only one thread can execute a synchronized argument of a given monitor. } 同期メソッドは相互排他的に、つまり同時にはただ1つのスレッドだけが与えられたモニターの同期化引数を実行できる方式で、その引数である計算 e を実行します。 #co(){ Threads can suspend inside a monitor by waiting on a signal. Threads that call the wait method wait until a notify method of the same object is called subsequently by some other thread. Calls to notify with no threads waiting for the signal are ignored. } スレッドはシグナルをウェイトすることで、モニター内でサスペンドできます。 wait メソッドを呼び出すスレッドは、同じオブジェクトの notify メソッドが他のスレッドからその後呼ばれるまで、ウェイトします。 #co(){ There is also a timed form of wait, which blocks only as long as no signal was received or the specified amount of time (given in milliseconds) has elapsed. Furthermore, there is a notifyAll method which unblocks all threads which wait for the signal. These methods, as well as class Monitor are primitive in Scala; they are implemented in terms of the underlying runtime system. } ウェイトの制限時間つき形態もあり、シグナルを受信するか、あるいは指定された時間(ミリ秒で与えられる)が過ぎるまでブロックします。さらにまた、シグナルを待つ全てのスレッドをアンブロックする notifyAll メソッドもあります。これらのメソッドは Monitor クラスと同様に、Scala ではプリミティブです。つまり、それらは実行時システムによって実装されています。 #co(){ Typically, a thread waits for some condition to be established. If the condition does not hold at the time of the wait call, the thread blocks until some other thread has established the condition. It is the responsibility of this other thread to wake up waiting processes by issuing a notify or notifyAll. Note however, that there is no guarantee that a waiting process gets to run immediately after the call to notify is issued. It could be that other processes get to run first which invalidate the condition again. Therefore, the correct form of waiting for a condition C uses a while loop: } 典型的には、スレッドはある条件が確立するまでウェイトします。もしその条件が wait をコールするまでに確立していなければ、そのスレッドは、他のスレッドがその条件を確立するまで、ブロックします。notify あるいは notifyAll を発行してウェイトしているプロセスを起動するのは、他のスレッドの責任です。しかし、ウェイトしているプロセスが notify 発行後すぐに実行されるという保証はありません。他のプロセスが最初に実行され、その条件を無効にすることもあり得ます。ですから、条件 C の確立をウェイとする正しい形は、while ループを使うことです。 while (!C ) wait() #co(){ As an example of how monitors are used, here is is an implementation of a bounded buffer class. } モニターの使用例として、境界付きバッファの実装をあげておきます。 class BoundedBuffer[A](N: Int) { var in = 0, out = 0, n = 0 val elems = new Array[A](N) def put(x: A) = synchronized { while (n >= N) wait() elems(in) = x ; in = (in + 1) % N ; n = n + 1 if (n == 1) notifyAll() } def get: A = synchronized { while (n == 0) wait() val x = elems(out) ; out = (out + 1) % N ; n = n - 1 if (n == N - 1) notifyAll() x } } #co(){ And here is a program using a bounded buffer to communicate between a producer and a consumer process. } 境界付きバッファを使って生産プロセスと消費プロセスとの間でコミュニケーションをとるプログラムを見てみましょう。 import scala.concurrent.ops._ ... val buf = new BoundedBuffer[String](10) spawn { while (true) { val s = produceString ; buf.put(s) } } spawn { while (true) { val s = buf.get ; consumeString(s) } } } #co(){ The spawn method spawns a new thread which executes the expression given in the parameter. It is defined in object concurrent.ops as follows. } spawn メソッドは、パラメータで与えられた式を実行する新しいスレッドを生成します。それはオブジェクト scala.concurrent.ops において、次のように定義されています。 def spawn(p: => Unit) { val t = new Thread() { override def run() = p } t.start() } #center(){[[前ページ>Chapter 17 Abstractions for Concurrency]] [[ 17 章>Chapter 17 Abstractions for Concurrency]] [[目次>ScalaByExample和訳]] [[次ページ>Example17.2]]} ---- #comment

表示オプション

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

下から選んでください:

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