Thread.stop() -- Why is it deprecated ? - What alternatives ?
Hi,
I'm creating an XMPP server in Java and I have a problem. I have a thread that run the SAX parser and is constantly blocking to recive data from my socket. Now the problem is that at some point (after the authentication) I need to stop the XML parsing.
The only way I can do this, I think, is by using the Thread.stop() method, but it is deprecated :(
I read the FAQ about this deprecation but the alternatives given can't work for me:
- I can't close the eocket because I need to read another XML document right after on the socket
- I can't call Thread.interrupt() because the thread is blocking on an IO
- I can't use a shared variable because the thread is blocked inside the SAX parser that call from time to time a callback. I can't modify the SAX code :(
Does anyone have an idea how to make my thread stop the parsing ?
Thanks
Mildred
[913 byte] By [
Mildreda] at [2007-11-26 22:29:15]

# 1
If you've read the "FAQ" on deprecation of stop etc then you already know why stop() is deprecated - it is inherently unsafe as you can't know for sure what your thread was doing when you tried to stop it. Further, what those documents don't tell you is that Thread.stop doesn't break you out of most blocking situations any way - so it wouldn't necessarily help even if it weren't deprecated.
I don't know the I/O or threading architecture of what you are working with so the following may not be applicable, but hopefully something will help:
One of the alternatives you didn't mention is setting the SO_TIMEOUT option on the socket so that your thread can periodically check if its actually been cancelled. I don't know if that is possible in this case it depends on how the use of sockets gets exposed by the library.
Other possibilities for unblocking a thread are to give the thread what it is waiting for - some data on the socket in this case. If you can send something that can be interpreted as "stop looking for more data", or if you check for cancellation before trying to interpret the data at all, then you can unblock the thread by writing to the socket. Whether this is feasible depends on how the real data is written to the socket.
Final possibility is to create a new thread to do the socket reading. Your parser thread can then read from a BlockingQueue, for example, that is populated with data from the socket thread. You can use interrupt() to cancel the parser thread and just ignore the socket thread - which presumably will unblock when the next real data arrives.
# 2
Thanks for your answer, it was very useful for me.
So I created a proxy InputStream, that read on a BlockingQueue on another thread (containing the Sockt). And When I want to restart the parser, I just interrupt the parser thread and the proxy InputStream throw a custom IOException handled later.
thank you for those ideas
Mildred