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: ...@@ -19,7 +19,7 @@ class ThreadReturning[A](toRun: => A) extends Thread:
join() join()
result result
def thread[A](toRun: => A): ThreadReturning[A] = def threadStart[A](toRun: => A): ThreadReturning[A] =
val t = ThreadReturning(toRun) val t = ThreadReturning(toRun)
t.start() t.start()
t t
...@@ -37,5 +37,5 @@ def countLots: Long = ...@@ -37,5 +37,5 @@ def countLots: Long =
// Run from the SBT shell with `runMain lecture1.scalaThreadWrapper`. // Run from the SBT shell with `runMain lecture1.scalaThreadWrapper`.
@main def scalaThreadWrapper: Unit = @main def scalaThreadWrapper: Unit =
val (x, y) = (thread(countLots), thread(countLots)) val (x, y) = (threadStart(countLots), threadStart(countLots))
println((x.joinMe, y.joinMe)) println((x.joinMe, y.joinMe))
...@@ -7,5 +7,7 @@ package lecture1 ...@@ -7,5 +7,7 @@ package lecture1
trait Task[A]: trait Task[A]:
def join: A def join: A
def task[A](toRun: => A): Task[A] = new Task[A]: def task[A](toRun: => A): Task[A] =
override def join: A = thread(toRun).joinMe val t = threadStart(toRun)
new Task[A]:
override def join: A = t.joinMe
package lecture6 package lecture6
import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext
import lecture1.thread import lecture1.threadStart
class OnePlaceBuffer[Elem]: class OnePlaceBuffer[Elem]:
var elem: Elem = _ var elem: Elem = _
...@@ -23,7 +23,7 @@ class OnePlaceBuffer[Elem]: ...@@ -23,7 +23,7 @@ class OnePlaceBuffer[Elem]:
@main def OnePlaceBufferDemo = @main def OnePlaceBufferDemo =
val buffer = OnePlaceBuffer[Int]() val buffer = OnePlaceBuffer[Int]()
val getThreads = for i <- 0 until 10 yield thread { buffer.get() } val getThreads = for i <- 0 until 10 yield threadStart { buffer.get() }
val putThreads = for i <- 0 until 10 yield thread { buffer.put(i) } val putThreads = for i <- 0 until 10 yield threadStart { buffer.put(i) }
putThreads.foreach(_.join()) putThreads.foreach(_.join())
getThreads.foreach(_.join()) getThreads.foreach(_.join())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment