forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputeserver.scala
54 lines (44 loc) · 1011 Bytes
/
computeserver.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package examples
import concurrent._, concurrent.ops._
class ComputeServer(n: Int) {
private trait Job {
type t
def task: t
def ret(x: t): Unit
}
private val openJobs = new Channel[Job]()
private def processor(i: Int) {
while (true) {
val job = openJobs.read
println("read a job")
job.ret(job.task)
}
}
def future[a](p: => a): () => a = {
val reply = new SyncVar[a]()
openJobs.write{
new Job {
type t = a
def task = p
def ret(x: a) = reply.set(x)
}
}
() => reply.get
}
//spawn(replicate(0, n) { processor })
spawn((0 until n).par foreach { processor })
}
object computeserver extends App {
def kill(delay: Int) = new java.util.Timer().schedule(
new java.util.TimerTask {
override def run() = {
println("[killed]")
sys exit 0
}
},
delay) // in milliseconds
val server = new ComputeServer(1)
val f = server.future(42)
println(f())
kill(10000)
}