Last active
March 13, 2026 21:30
-
-
Save klaeufer/5409800 to your computer and use it in GitHub Desktop.
I/O-bound tasks for experimenting in the Scala REPL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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