Anonymous user
Synchronous concurrency: Difference between revisions
→Using threads: Add a join, otherwise there is a race issue.
(Added Wren) |
(→Using threads: Add a join, otherwise there is a race issue.) |
||
Line 1,491:
===Using threads===
<lang kotlin>import java.util.concurrent.SynchronousQueue▼
▲import java.util.concurrent.SynchronousQueue
import kotlin.concurrent.thread
import java.io.File
val queue = SynchronousQueue<String>()▼
const val EOT = "\u0004" // end of transmission
fun main(args: Array<String>) {
▲ val queue = SynchronousQueue<String>()
thread {▼
▲ val work = thread {
var count = 0
Line 1,518 ⟶ 1,516:
File("input.txt").forEachLine { line -> queue.put(line) }
queue.put(EOT)
work.join()
val count = queue.take().toInt()
println("\nNumber of lines printed = $count")
|