Jump to content

Synchronous concurrency: Difference between revisions

→‎Using coroutines: Use forEachLine over useLines; No need to close the file.
(→‎Using threads: Add a join, otherwise there is a race issue.)
(→‎Using coroutines: Use forEachLine over useLines; No need to close the file.)
Line 1,542:
===Using coroutines===
Uses experimental features and will change in the future. Same output as the threads version.
<lang kotlin>// version 1.3.20 with kotlinx-coroutines-core version 1.1.1
<lang scala>
// version 1.3.20 with kotlinx-coroutines-core version 1.1.1
 
import kotlinx.coroutines.async
Line 1,562 ⟶ 1,561:
}
 
File("input.txt").bufferedReader().useLinesforEachLine { textline -> lines.send(line) }
for (line in text) {
lines.send(line)
}
}
lines.close()
println("\nNumber of lines printed = ${count.await()}")
}
}</lang scala>
}
</lang>
 
=={{header|Logtalk}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.