SHARE
TWEET

Untitled

a guest Sep 11th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. object ConnectedStreamsApp extends App {
  2.   def writer(outputStream: OutputStream): Unit = {
  3.     val writer = new PrintStream(outputStream)
  4.     (1 to 1000).foreach { i =>
  5.       writer.println(s"line $i")
  6.       Thread.sleep(50)
  7.     }
  8.     writer.close()
  9.   }
  10.  
  11.   def reader(inputStream: InputStream): Unit = {
  12.     val reader = new BufferedReader(new InputStreamReader(inputStream))
  13.     while (true) {
  14.       var line = reader.readLine()
  15.       if (line == null) {
  16.         reader.close()
  17.         return
  18.       }
  19.       println(s"read: $line")
  20.     }
  21.   }
  22.  
  23.   val in  = new PipedInputStream(4024)
  24.   val out = new PipedOutputStream(in)
  25.  
  26.   val writerThread = new Thread(() => writer(out))
  27.   writerThread.start()
  28.   reader(in)
  29.   writerThread.join()
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top