Skip to content
Snippets Groups Projects
Commit 63c678ec authored by Matt Bovel's avatar Matt Bovel
Browse files

Fix task implementation, rename thread to threadStart

parent 797a83b5
No related branches found
No related tags found
No related merge requests found
......@@ -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))
......@@ -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
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())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment