Is setting a timeout on ObjectInputStream.readObject() safe?

后端 未结 2 1239
忘掉有多难
忘掉有多难 2021-01-14 17:45

I have an ObjectInputStream connected to an ObjectOutputStream through a socket, and I\'ve been using Socket.setSoTimeout() to make

2条回答
  •  萌比男神i
    2021-01-14 18:17

    I have a thread constantly getting new data through this function but I want to be able to stop it by setting a boolean to false. The thread has to keep polling the boolean and can't if it's blocked by readObject()

    You can use Thread.interrupt to let it throw an InterruptedException, or in this case an InterruptedIOException. Make sure you don't swallow exceptions!

提交回复
热议问题