From 63c678ec73459d5b7b9658a68395af4195a733b2 Mon Sep 17 00:00:00 2001 From: Matt Bovel <matthieu@bovel.net> Date: Wed, 29 Mar 2023 17:36:43 +0200 Subject: [PATCH] Fix task implementation, rename thread to threadStart --- src/main/scala/lecture1/02-scalaThreadWrapper.scala | 4 ++-- src/main/scala/lecture1/04-task.scala | 6 ++++-- src/main/scala/lecture6/04-OnePlaceBufferDemo.scala | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/scala/lecture1/02-scalaThreadWrapper.scala b/src/main/scala/lecture1/02-scalaThreadWrapper.scala index 2c64f04..bb66082 100644 --- a/src/main/scala/lecture1/02-scalaThreadWrapper.scala +++ b/src/main/scala/lecture1/02-scalaThreadWrapper.scala @@ -19,7 +19,7 @@ class ThreadReturning[A](toRun: => A) extends Thread: join() result -def thread[A](toRun: => A): ThreadReturning[A] = +def threadStart[A](toRun: => A): ThreadReturning[A] = val t = ThreadReturning(toRun) t.start() t @@ -37,5 +37,5 @@ def countLots: Long = // Run from the SBT shell with `runMain lecture1.scalaThreadWrapper`. @main def scalaThreadWrapper: Unit = - val (x, y) = (thread(countLots), thread(countLots)) + val (x, y) = (threadStart(countLots), threadStart(countLots)) println((x.joinMe, y.joinMe)) diff --git a/src/main/scala/lecture1/04-task.scala b/src/main/scala/lecture1/04-task.scala index 2dcb09c..e14d04b 100644 --- a/src/main/scala/lecture1/04-task.scala +++ b/src/main/scala/lecture1/04-task.scala @@ -7,5 +7,7 @@ package lecture1 trait Task[A]: def join: A -def task[A](toRun: => A): Task[A] = new Task[A]: - override def join: A = thread(toRun).joinMe +def task[A](toRun: => A): Task[A] = + val t = threadStart(toRun) + new Task[A]: + override def join: A = t.joinMe diff --git a/src/main/scala/lecture6/04-OnePlaceBufferDemo.scala b/src/main/scala/lecture6/04-OnePlaceBufferDemo.scala index 8f05989..2c42755 100644 --- a/src/main/scala/lecture6/04-OnePlaceBufferDemo.scala +++ b/src/main/scala/lecture6/04-OnePlaceBufferDemo.scala @@ -1,7 +1,7 @@ package lecture6 import scala.concurrent.ExecutionContext -import lecture1.thread +import lecture1.threadStart class OnePlaceBuffer[Elem]: var elem: Elem = _ @@ -23,7 +23,7 @@ class OnePlaceBuffer[Elem]: @main def OnePlaceBufferDemo = val buffer = OnePlaceBuffer[Int]() - val getThreads = for i <- 0 until 10 yield thread { buffer.get() } - val putThreads = for i <- 0 until 10 yield thread { buffer.put(i) } + val getThreads = for i <- 0 until 10 yield threadStart { buffer.get() } + val putThreads = for i <- 0 until 10 yield threadStart { buffer.put(i) } putThreads.foreach(_.join()) getThreads.foreach(_.join()) -- GitLab