如何为阻塞调用设置超时?

How can I set a timeout for a blocking call?

我有一个与多个客户端一起工作的多线程聊天服务器(每个客户端都在一个新线程中处理)。

如果客户端断开连接,服务器上的相关线程将挂起 inputstream.readLine(),这是一个阻塞调用。

如何设置某种超时,以便在 2000 毫秒后关闭套接字并在没有响应的情况下释放线程?

class Handler(socket: Socket) extends Runnable {
  def run() : Unit = {
    val inputstream = new BufferedReader(new InputStreamReader(socket.getInputStream()))
    val outputstream = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))
    var break = false
    while(break != true){
      val input = inputstream.readLine() // blocking call
// how do i time out after 2000ms?
      input match {
        case "update" =>
          outputstream.write("message")
          outputstream.newLine()
          outputstream.flush()
        case _ => // any other input, break and close socket
          break = true
      }
    }
    socket.close()
  }
}

object ChatServer extends App {
  val server = new ServerSocket(10000)
  while(true) {
    val socket = server.accept // blocking call
    (new Thread(new Handler(socket))).start()
    // some condition{
    //   server.close()
    // }
  }
}

您可以使用 BufferedReader.ready():

True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.

class Handler(socket: Socket) extends Runnable {
  def run(): Unit = {
    val inputstream = new BufferedReader(new InputStreamReader(socket.getInputStream))
    val outputstream = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream))

    var break = false
    val startTime = System.currentTimeMillis()

    while (!break) {
      if (inputstream.ready()) {
        val input = inputstream.readLine()
        input match {
          case "update" =>
            outputstream.write("message")
            outputstream.newLine()
            outputstream.flush()
          case _ => // any other input, break and close socket
            break = true
        }
      } else if (System.currentTimeMillis() - startTime >= 2000) {
        break = true
      }
    }
    socket.close()
  }
}