blob: 9f5eb74ab66bfcf6dc40892c5d8d859d5f414de2 (
plain) (
blame)
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
|
/* NSC -- new Scala compiler
* Copyright 2005-2011 LAMP/EPFL
* @author Paul Phillips
*/
package scala.tools.nsc
import java.util.concurrent.{ Future, Callable, Executors, ThreadFactory }
import java.util.{ Timer, TimerTask }
package object io {
def runnable(body: => Unit): Runnable = new Runnable { override def run() = body }
def callable[T](body: => T): Callable[T] = new Callable[T] { override def call() = body }
def spawn[T](body: => T): Future[T] = Executors.newSingleThreadExecutor() submit callable[T](body)
def submit(runnable: Runnable) = Executors.newSingleThreadExecutor() submit runnable
def runnableFn(f: () => Unit): Runnable = runnable(f())
def callableFn[T](f: () => T): Callable[T] = callable(f())
def spawnFn[T](f: () => T): Future[T] = spawn(f())
def newConfiguredExecutor(f: Thread => Unit) = {
Executors.newCachedThreadPool(new ThreadFactory {
def newThread(r: Runnable) = {
val t = Executors.defaultThreadFactory().newThread(r)
f(t)
t
}
})
}
def newDaemonThreadExecutor() = newConfiguredExecutor(_ setDaemon true)
// Create, start, and return a background thread
// If isDaemon is true, it is marked as daemon (and will not interfere with JVM shutdown)
def daemonize(isDaemon: Boolean)(body: => Unit): Thread = {
val thread = new Thread(runnable(body))
thread setDaemon isDaemon
thread.start
thread
}
// Set a timer to execute the given code.
def timer(seconds: Int)(body: => Unit): Timer = {
val alarm = new Timer(true) // daemon
val tt = new TimerTask { def run() = body }
alarm.schedule(tt, seconds * 1000)
alarm
}
}
|