3

I would like to kill programatically a thread which is running in a while(true) loop and listening for connections infinetely and since listening is a blocking method I cant simply set the loops statemenet into false. My application is generally swapping networks periodically when connected launches a server to listen for connections, so when it does swap to another network I am afraid that the old server will become a zombie and just stuck there since on the new network the server is bound on different IP than the one it had previously.

PS. will binding the server to localhost not require its reset after network change?

Miky
  • 942
  • 2
  • 14
  • 29
  • First thing I would recommend is to refactor your code to non-blocking calls, if that's possible in your language (btw you didn't mention which one). Then you could just break the loop... EDIT: sorry I've just noticed it is in Java category. – Dalibor Filus Dec 06 '11 at 19:18
  • @NoICE the language is in the tags, JAVA :) , how to refactor a method like socket.accept()? Is that even possible? – Miky Dec 06 '11 at 19:20
  • Ruby has IO.select call instead of accept.. and I would bet that java has some other method to do that too... Just google "java non blocking io" (or socket) (I would like to help more with this, but I'm beginner in java) – Dalibor Filus Dec 06 '11 at 19:24

3 Answers3

4

If you need to be able to stop a thread sitting in an accept loop, you can just call setSoTimeout so that the accept call can complete without waiting for an actual connection to happen.

Note that if you bind to all interfaces, you probably don't need to create a new socket when a new network interface is added to the system.

Other possibilities are calling close on the socket, or stop or interrupt on the thread. If you can use async I/O, see http://www.developer.com/java/article.php/3837316/Non-Blocking-IO-Made-Possible-in-Java.htm.

Gabe
  • 84,912
  • 12
  • 139
  • 238
1

Simply close the ServerSocketChannel and the thread calling accept() will either throw AsynchronousCloseException or ClosedChannelException.

Another possibility would be to interrupt the thread. According to the JDK documentation it should then throw ClosedByInterruptException when calling accept() and thereby close the channel. However, in my experience the ClosedByInterruptException thing didn't always work reliable and there are known bugs in certain JDK versions.

Gerald Thaler
  • 569
  • 1
  • 5
  • 8
0

Did you google the question? There's a lot about how to stop threads in your code.

Sun's documentation has what you need here:

http://docs.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

And here's a related question where I got this from:

How do you kill a thread in Java?

please look around SO before asking a question.

Community
  • 1
  • 1
Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
  • yes I have done some research before posting, my question is not as simple as just killing a thread which btw its risky in Java because in my situation it will most likely turn into a zombie process. – Miky Dec 06 '11 at 19:15