summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraleksandar <aleksandar@lampmac14.epfl.ch>2011-12-12 18:17:58 +0100
committeraleksandar <aleksandar@lampmac14.epfl.ch>2011-12-12 18:17:58 +0100
commit249aa978e7900a792594d51b45f4560568f1552c (patch)
tree057f142a07b8643294b74f06d853a4836f45b939
parent7021aef3fd8a20c8f730af36f229e7bb2cfe8fb5 (diff)
parentc2a52307bf60ca9d8b8d4980d8594284ff320dab (diff)
downloadscala-249aa978e7900a792594d51b45f4560568f1552c.tar.gz
scala-249aa978e7900a792594d51b45f4560568f1552c.tar.bz2
scala-249aa978e7900a792594d51b45f4560568f1552c.zip
Merge branch 'execution-context' of github.com:phaller/scala into execution-context
Conflicts: test/files/jvm/concurrent-future.scala
-rw-r--r--src/library/scala/concurrent/Scheduler.scala54
-rw-r--r--src/library/scala/concurrent/default/SchedulerImpl.scala44
-rw-r--r--src/library/scala/concurrent/package.scala14
-rw-r--r--test/files/jvm/concurrent-future.scala15
4 files changed, 116 insertions, 11 deletions
diff --git a/src/library/scala/concurrent/Scheduler.scala b/src/library/scala/concurrent/Scheduler.scala
new file mode 100644
index 0000000000..39d798e6b4
--- /dev/null
+++ b/src/library/scala/concurrent/Scheduler.scala
@@ -0,0 +1,54 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+package scala.concurrent
+
+import scala.util.Duration
+
+/** A service for scheduling tasks and thunks for one-time, or periodic execution.
+ */
+trait Scheduler {
+
+ /** Schedules a thunk for repeated execution with an initial delay and a frequency.
+ *
+ * @param delay the initial delay after which the thunk should be executed
+ * the first time
+ * @param frequency the frequency with which the thunk should be executed,
+ * as a time period between subsequent executions
+ */
+ def schedule(delay: Duration, frequency: Duration)(thunk: => Unit): Cancellable
+
+ /** Schedules a task for execution after a given delay.
+ *
+ * @param delay the duration after which the task should be executed
+ * @param task the task that is scheduled for execution
+ * @return a `Cancellable` that may be used to cancel the execution
+ * of the task
+ */
+ def scheduleOnce(delay: Duration, task: Runnable): Cancellable
+
+ /** Schedules a thunk for execution after a given delay.
+ *
+ * @param delay the duration after which the thunk should be executed
+ * @param thunk the thunk that is scheduled for execution
+ * @return a `Cancellable` that may be used to cancel the execution
+ * of the thunk
+ */
+ def scheduleOnce(delay: Duration)(task: => Unit): Cancellable
+
+}
+
+
+
+trait Cancellable {
+
+ /** Cancels the underlying task.
+ */
+ def cancel(): Unit
+
+}
diff --git a/src/library/scala/concurrent/default/SchedulerImpl.scala b/src/library/scala/concurrent/default/SchedulerImpl.scala
new file mode 100644
index 0000000000..745d2d1a15
--- /dev/null
+++ b/src/library/scala/concurrent/default/SchedulerImpl.scala
@@ -0,0 +1,44 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+package scala.concurrent
+package default
+
+import scala.util.Duration
+
+private[concurrent] final class SchedulerImpl extends Scheduler {
+ private val timer =
+ new java.util.Timer(true) // the associated thread runs as a daemon
+
+ def schedule(delay: Duration, frequency: Duration)(thunk: => Unit): Cancellable = ???
+
+ def scheduleOnce(delay: Duration, task: Runnable): Cancellable = {
+ val timerTask = new java.util.TimerTask {
+ def run(): Unit =
+ task.run()
+ }
+ timer.schedule(timerTask, delay.toMillis)
+ new Cancellable {
+ def cancel(): Unit =
+ timerTask.cancel()
+ }
+ }
+
+ def scheduleOnce(delay: Duration)(task: => Unit): Cancellable = {
+ val timerTask = new java.util.TimerTask {
+ def run(): Unit =
+ task
+ }
+ timer.schedule(timerTask, delay.toMillis)
+ new Cancellable {
+ def cancel(): Unit =
+ timerTask.cancel()
+ }
+ }
+
+}
diff --git a/src/library/scala/concurrent/package.scala b/src/library/scala/concurrent/package.scala
index dbe2f90f18..d93d5b04ba 100644
--- a/src/library/scala/concurrent/package.scala
+++ b/src/library/scala/concurrent/package.scala
@@ -16,17 +16,24 @@ import scala.util.{ Timeout, Duration }
-/** This package object contains primitives for parallel programming.
+/** This package object contains primitives for concurrent and parallel programming.
*/
package object concurrent {
- type ExecutionException = java.util.concurrent.ExecutionException
+ type ExecutionException = java.util.concurrent.ExecutionException
type CancellationException = java.util.concurrent.CancellationException
- type TimeoutException = java.util.concurrent.TimeoutException
+ type TimeoutException = java.util.concurrent.TimeoutException
+ /** A global execution environment for executing lightweight tasks.
+ */
lazy val executionContext =
new default.ExecutionContextImpl
+ /** A global service for scheduling tasks for execution.
+ */
+ lazy val scheduler =
+ new default.SchedulerImpl
+
private[concurrent] def currentExecutionContext: ThreadLocal[ExecutionContext] = new ThreadLocal[ExecutionContext] {
override protected def initialValue = null
}
@@ -83,6 +90,7 @@ package object concurrent {
}
+
package concurrent {
/** A timeout exception.
diff --git a/test/files/jvm/concurrent-future.scala b/test/files/jvm/concurrent-future.scala
index 8fb237eb0a..eb3bbad591 100644
--- a/test/files/jvm/concurrent-future.scala
+++ b/test/files/jvm/concurrent-future.scala
@@ -44,7 +44,7 @@ object Test extends App {
def testOnSuccessWhenFailed(): Unit = once {
done =>
- val f = future {
+ val f = future[Unit] {
output(3, "hai world")
done()
throw new Exception
@@ -56,7 +56,7 @@ object Test extends App {
def testOnFailure(): Unit = once {
done =>
- val f = future {
+ val f = future[Unit] {
output(4, "hai world")
throw new Exception
}
@@ -64,16 +64,15 @@ object Test extends App {
output(4, "onoes")
done()
}
- f onFailure {
- case _ =>
- output(4, "kthxbye")
- done()
+ f onFailure { case _ =>
+ output(4, "kthxbye")
+ done()
}
}
def testOnFailureWhenSpecialThrowable(num: Int, cause: Throwable): Unit = once {
done =>
- val f = future {
+ val f = future[Unit] {
output(num, "hai world")
throw cause
}
@@ -93,7 +92,7 @@ object Test extends App {
def testOnFailureWhenFutureTimeoutException(): Unit = once {
done =>
- val f = future {
+ val f = future[Unit] {
output(8, "hai world")
throw new FutureTimeoutException(null)
}