summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-08-15 17:02:05 +0000
committerPaul Phillips <paulp@improving.org>2011-08-15 17:02:05 +0000
commit4f4b85e62229f701b06e8e6a65f50360806c6d22 (patch)
treee4c6b2f21f937f42ab54716ba0acdcdd7d8b6a2a
parent5bae04edd7dd96070622d5801bf444f694f2063f (diff)
downloadscala-4f4b85e62229f701b06e8e6a65f50360806c6d22.tar.gz
scala-4f4b85e62229f701b06e8e6a65f50360806c6d22.tar.bz2
scala-4f4b85e62229f701b06e8e6a65f50360806c6d22.zip
(For SI-4705) Some tweaks to repl thread creati...
(For SI-4705) Some tweaks to repl thread creation based on speculation from mark harrah, no review.
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/Line.scala2
-rw-r--r--src/compiler/scala/tools/nsc/io/DaemonThreadFactory.scala21
-rw-r--r--src/compiler/scala/tools/nsc/io/package.scala27
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
}