File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Interrupt a thread blocking on IO Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Interrupt a thread blocking on IO" Watch "Interrupt a thread blocking on IO" New topic
Author

Interrupt a thread blocking on IO

Brian Podolny
Ranch Hand

Joined: Aug 29, 2000
Posts: 32
How can you interrupt a thread that is blocking on IO.
I have a thread which is in an infinite loop, which keeps calling ServerSocket.accept();
Interrupting this thread does nothing until it receives a client connection because it is blocked on the accept() method.
Any suggestions?
Thanks,
Brian Podolny
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi!
try that...

[This message has been edited by Pierre-Fran�ois Lemosquet (edited August 28, 2001).]
Brian Podolny
Ranch Hand

Joined: Aug 29, 2000
Posts: 32
Thanks for the reply but I don't think that solves the problem. The thread is still blocked on the accept(). The stop() method is the right way to shut down but it will not be invoked.
Maybe I'm missing something...
Xavier Casals
Greenhorn

Joined: Aug 16, 2001
Posts: 4
Have you tried to use 'setSoTimeout'? It interrupts accept() when timeout expires. Then you can decide if you want to finish or not. Is like an active wait ... but I don't know if there is a better solution.
I have never programmed with sockets in Java, but I think it can work ...
Brian Podolny
Ranch Hand

Joined: Aug 29, 2000
Posts: 32
That's a good solution Xavier. Thanks. I'm not crazy about the active wait, so I 'll keep thinking on it but if I can't come up with anything than your solution is my way to go.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Interrupt a thread blocking on IO
 
Similar Threads
Thread Intrerupt
Thread
Dan's Thread
simple Q about thread.interrupt()
MultiThreaded Programming: Killing Thread