Skip to content

Instantly share code, notes, and snippets.

@klaeufer
Last active March 13, 2026 21:30
Show Gist options
  • Select an option

  • Save klaeufer/5409800 to your computer and use it in GitHub Desktop.

Select an option

Save klaeufer/5409800 to your computer and use it in GitHub Desktop.
I/O-bound tasks for experimenting in the Scala REPL
def p(s: String) =
for i <- 1 to 10 do
Thread.sleep(500)
println(s"$i : $s")
// running two or more tasks sequentially
// about 20 seconds by design
val t0 = System.currentTimeMillis
p("a")
p("b")
p("c")
p("d")
System.currentTimeMillis - t0
// running two tasks or more concurrently
// down to about about 5 seconds
def makeP(label: String) = new java.lang.Thread:
override def run() = p(label)
val p1 = makeP("a")
val p2 = makeP("b")
val p3 = makeP("b")
val p4 = makeP("b")
val t1 = System.currentTimeMillis
p1.start()
p2.start()
p3.start()
p4.start()
p1.join()
p2.join()
p3.join()
p4.join()
System.currentTimeMillis - t1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment