Scala by Example:scala 2.11.7中的spawn函数

时间:2015-12-22 07:36:43

标签: scala

我正在尝试使用Scala版本2.11.7实现第17.9节Scala示例书中的工作者。 导入声明:

import scala.concurrent._, scala.concurrent.ops._

错误" ops不是scala.concurrent"的成员。我确实谷歌并知道concurrent.ops已被弃用,而不是将来,将import语句更改为:

import scala.concurrent._, scala.concurrent.Future._

全班来源:

import scala.concurrent._
import scala.concurrent.Future._

class ComputeServer(n: Int) {

  private abstract class Job {
    type T
    def task: T
    def res(x: T)
  }

  private val openJobs = new Channel[Job]()

  private def processor(i: Int) {
    while(true) {
      val job = openJobs.read
      job.res(job.task)
    }
  }

  def future[A](p: => A): () => A = {
    val reply = new SyncVar[A]()
    openJobs.write{
      new Job{
        type T = A
        def task = p
        def res(x: A) = reply.put(x)
      }
    }
    () => reply.get
  }

  spawn(replicate(0, n){processor})
}

但是在行中发生错误:spawn(replicate(0, n){processor})

not found: value spawn
not found: value replicate
missing arguments for method processor in class ComputeServer; follow this method with `_' if you want to treat it as a partially applied function

版本2.11.7中的“生成,复制,处理器功能”是什么?

1 个答案:

答案 0 :(得分:1)

可以像这样创建方法spawn:

def spawn(p: => Unit) {
   val t = new Thread() { override def run() = p }
   t.start()
}