アットウィキロゴ

Example17.2

「Example17.2」の編集履歴(バックアップ)一覧に戻る

Example17.2 - (2011/02/24 (木) 09:12:33) のソース

#co(){
17.2 SyncVars

A synchronized variable (or syncvar for short) offers get and put operations to read and set the variable. get operations block until the variable has been defined. An unset operation resets the variable to undefined state.

Here's the standard implementation of synchronized variables.
}


#setmenu2(ex-r-menu)
** 17.2 同期変数

同期化された変数(あるいは縮めて、同期変数)は変数の読み出しと設定用に、get と put 操作を提供します。get 操作は、変数が定義されるまでブロックします。unset 操作は、変数を未定義状態にリセットします。

次は同期変数の標準的な実装です。

  package scala.concurrent
  class SyncVar[A] {
    private var isDefined: Boolean = false
    private var value: A = _
    def get = synchronized {
      while (!isDefined) wait()
      value
    }
    def set(x: A) = synchronized {
      value = x; isDefined = true; notifyAll()
    }
    def isSet: Boolean = synchronized {
      isDefined
    }
    def unset = synchronized {
      isDefined = false
    }
  }

#center(){[[前ページ>Example17.1]] [[ 17 章>Chapter 17 Abstractions for Concurrency]] [[目次>ScalaByExample和訳]] [[次ページ>Example17.3]]}

----
#comment
ツールボックス

下から選んでください:

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