Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop/Interrupt threads blocked on waiting input from socket

As the title says I need a way to stop or interrupt a thread that is blocked waiting on an input from the socket.

like image 335
klaus johan Avatar asked Jun 21 '09 18:06

klaus johan


2 Answers

You could simply close() the IO stream/socket from another thread. Then, you could check on a volatile boolean field if the resulting IOException is due the close or something else. I think the close might result in an java.net.SocketException: Socket closed exception.

like image 169
akarnokd Avatar answered Sep 18 '22 06:09

akarnokd


Thread.interrupt() should be the method you're looking for. Be sure that your input-accessing methods also check for InterruptedException and ClosedByInterruptException.

Also look at the corresponding page in the Sun Concurrency Tutorial, as another solution suggests.

like image 40
Kosi2801 Avatar answered Sep 20 '22 06:09

Kosi2801