~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to docs/examples/computeserver.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package examples
2
 
 
3
 
import concurrent._, concurrent.ops._
4
 
 
5
 
class ComputeServer(n: Int) {
6
 
 
7
 
  private trait Job {
8
 
    type t
9
 
    def task: t
10
 
    def ret(x: t): Unit
11
 
  }
12
 
 
13
 
  private val openJobs = new Channel[Job]()
14
 
 
15
 
  private def processor(i: Int): Unit = {
16
 
    while (true) {
17
 
      val job = openJobs.read
18
 
      println("read a job")
19
 
      job.ret(job.task)
20
 
    }
21
 
  }
22
 
 
23
 
  def future[a](p: => a): () => a = {
24
 
    val reply = new SyncVar[a]()
25
 
    openJobs.write{
26
 
      new Job {
27
 
        type t = a
28
 
        def task = p
29
 
        def ret(x: a) = reply.set(x)
30
 
      }
31
 
    }
32
 
    () => reply.get
33
 
  }
34
 
 
35
 
  spawn(replicate(0, n) { processor })
36
 
}
37
 
 
38
 
object computeserver extends Application {
39
 
 
40
 
  def kill(delay: Int) = new java.util.Timer().schedule(
41
 
    new java.util.TimerTask {
42
 
      override def run() = {
43
 
        println("[killed]")
44
 
        System.exit(0)
45
 
      }
46
 
    },
47
 
    delay) // in milliseconds
48
 
 
49
 
  val server = new ComputeServer(1)
50
 
  val f = server.future(42)
51
 
  println(f())
52
 
  kill(10000)
53
 
}