0

I have a client that receive cyclically data form a server

while (true) {
     System.out.println(in.readLine());
}

I want to set a timer of 5 sec to exit from the loop when the time is expired. I have search a lot but I haven not found anything that can help me.

I have tried with the Timer class but I found only example to schedule the execution of a task.

Another thing I tried is to use System.currentTimeMillis() but since the in.readLine() is blocking I can not break the loop and go above with the execution.

Thank you!

user2864740
  • 60,010
  • 15
  • 145
  • 220
Marco Mac
  • 79
  • 2
  • 10

1 Answers1

1

You can set the socket's setSoTimeout(int) to timeout the reading of input stream.

where int is in millisecond

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63