I want to build a simple server in Scala 2.11 that listens on a socket. It should asynchronously read data from the socket and pass the data into a Observable from RxScala.
I have a Java ServerSocket
from which the data should be read with the method readData
which is blocking. This method is started once and runs until the whole program stops:
val server = new ServerSocket(port)
def readData(server: ServerSocket): Unit = ???
I found two different ways to avoid blocking the whole program, when the data is read from the socket:
new Thread {
override def run(): Unit = {
readData(server)
}
}.start()
and
Future {
blocking {
readData(server)
}
}
Because there is no return value wrapped in the Future, which can then be passed to other tasks, the only task for the Future is to make the computation non blocking. So I wonder if there are any larger differences between these approaches? Looking into implementation of the Future it looks like it also creates and runs a Runnable with the given block. So is one of these approaches preferable if one has a single and forever/long running task without a result?