If I have java.net.HttpURLConnection that receives data (or is going to receive data) in a workflow, is it safe to call disconnect() to stop it at any point in the connection's life cycle in another thread?
I'm curious about this because I could not find a clearly documented way to end the current HTTP connection. Interrupting a workflow by calling Thread.interrupt() will not work because the InputStream that you get from the HttpURLConnection is not interrupted.
I did some experiments that look like this:
This seems to work, and both the connection and the socket object it creates will be eventually cleared by gc. However, I wonder if this is the right thing to do? Will there be a problem with calling disconnect() from another thread?
source share