Skip to content

Instantly share code, notes, and snippets.

@sortega
Created August 31, 2019 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sortega/d000a65f03f9354866adbf884863501d to your computer and use it in GitHub Desktop.
Save sortega/d000a65f03f9354866adbf884863501d to your computer and use it in GitHub Desktop.
object ConnectedStreamsApp extends App {
def writer(outputStream: OutputStream): Unit = {
val writer = new PrintStream(outputStream)
(1 to 1000).foreach { i =>
writer.println(s"line $i")
Thread.sleep(50)
}
writer.close()
}
def reader(inputStream: InputStream): Unit = {
val reader = new BufferedReader(new InputStreamReader(inputStream))
while (true) {
var line = reader.readLine()
if (line == null) {
reader.close()
return
}
println(s"read: $line")
}
}
val in = new PipedInputStream(4024)
val out = new PipedOutputStream(in)
val writerThread = new Thread(() => writer(out))
writerThread.start()
reader(in)
writerThread.join()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment