gpt4 book ai didi

Scala 示例 : spawn function in scala 2. 11.7

转载 作者:行者123 更新时间:2023-12-02 03:19:30 27 4
gpt4 key购买 nike

我正在尝试使用 Scala 版本 2.11.7 实现第 17.9 节《Scala 中的 worker 》。导入语句:

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

是错误“ops 不是 scala.concurrent 的成员”。我搜索了谷歌,知道 concurrent.ops 已被弃用,而在未来,将导入语句更改为:

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 版本中的 spawn、replicate、processor 函数是什么?

最佳答案

可以像这样创建方法 spawn:

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

关于Scala 示例 : spawn function in scala 2. 11.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34410677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com