Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala by Example: spawn function in scala 2.11.7

Tags:

scala

I'm trying implement section 17.9 Workers in Scala by Example book with Scala version 2.11.7. Import statements:

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

was error "ops not is member of scala.concurrent". I did google and known that concurrent.ops is deprecated and rather by future, change import statement to:

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

Entire class source:

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})
}

But occurs errors in line: 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

What're spawn, replicate, processor function in version 2.11.7?

like image 760
code4f Avatar asked Dec 14 '25 19:12

code4f


1 Answers

one can create method spawn like this:

def spawn(p: => Unit) {
   val t = new Thread() { override def run() = p }
   t.start()
}
like image 67
Mr. Mason Avatar answered Dec 19 '25 01:12

Mr. Mason