diff options
author | Paul Phillips <paulp@improving.org> | 2011-07-27 21:00:07 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-07-27 21:00:07 +0000 |
commit | 209167a1b4905f15898235b94d8312a51c312785 (patch) | |
tree | 27e4c8847e6160755e04cbdb55e966bb27f5e2f2 /src | |
parent | 321ecd84d8970acc43523280267595567b9a0ecb (diff) | |
download | scala-209167a1b4905f15898235b94d8312a51c312785.tar.gz scala-209167a1b4905f15898235b94d8312a51c312785.tar.bz2 scala-209167a1b4905f15898235b94d8312a51c312785.zip |
Some tweaks to repl thread creation based on sp...
Some tweaks to repl thread creation based on speculation from mark
harrah, no review.
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/interpreter/Line.scala | 2 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/io/DaemonThreadFactory.scala | 21 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/io/package.scala | 27 |
3 files changed, 31 insertions, 19 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/Line.scala b/src/compiler/scala/tools/nsc/interpreter/Line.scala index 79bd802694..deaeb913d2 100644 --- a/src/compiler/scala/tools/nsc/interpreter/Line.scala +++ b/src/compiler/scala/tools/nsc/interpreter/Line.scala @@ -38,7 +38,7 @@ class Line[+T](val code: String, body: => T) { private def cancel() = if (running) setState(Cancelled) // This is where the line thread is created and started. - private val _thread = io.daemonize(true) { + private val _thread = io.daemonize { try { _result = body setState(Done) diff --git a/src/compiler/scala/tools/nsc/io/DaemonThreadFactory.scala b/src/compiler/scala/tools/nsc/io/DaemonThreadFactory.scala new file mode 100644 index 0000000000..fa1218cc82 --- /dev/null +++ b/src/compiler/scala/tools/nsc/io/DaemonThreadFactory.scala @@ -0,0 +1,21 @@ +/* NSC -- new Scala compiler + * Copyright 2005-2011 LAMP/EPFL + * @author Paul Phillips + */ + +package scala.tools.nsc +package io + +import java.util.concurrent._ + +class DaemonThreadFactory extends ThreadFactory { + def newThread(r: Runnable): Thread = { + val thread = new Thread(r) + thread setDaemon true + thread + } +} + +object DaemonThreadFactory { + def newPool() = Executors.newCachedThreadPool(new DaemonThreadFactory) +}
\ No newline at end of file diff --git a/src/compiler/scala/tools/nsc/io/package.scala b/src/compiler/scala/tools/nsc/io/package.scala index 565a3d4bcb..2c5e50e970 100644 --- a/src/compiler/scala/tools/nsc/io/package.scala +++ b/src/compiler/scala/tools/nsc/io/package.scala @@ -5,7 +5,7 @@ package scala.tools.nsc -import java.util.concurrent.{ Future, Callable, Executors, ThreadFactory } +import java.util.concurrent.{ Future, Callable } import java.util.{ Timer, TimerTask } import java.util.jar.{ Attributes } @@ -13,36 +13,27 @@ package object io { type JManifest = java.util.jar.Manifest type JFile = java.io.File private[io] implicit def installManifestOps(m: JManifest) = new ManifestOps(m) + class ManifestOps(manifest: JManifest) { def attrs = manifest.getMainAttributes() def apply(name: Attributes.Name) = "" + attrs.get(name) def update(key: Attributes.Name, value: String) = attrs.put(key, value) } + private lazy val daemonThreadPool = DaemonThreadFactory.newPool() + 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 spawn[T](body: => T): Future[T] = daemonThreadPool submit callable(body) + def submit(runnable: Runnable) = daemonThreadPool 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 = { + // Create, start, and return a daemon thread + def daemonize(body: => Unit): Thread = { val thread = new Thread(runnable(body)) - thread setDaemon isDaemon + thread setDaemon true thread.start thread } |