multithreading - Interrupt the thread which is making HTTP call in Java -


I am writing a Java program to make an HTTP call in a separate thread. The following code I am implementing in a separate call-able function in the thread pool provider.

  MyRunnable Public Stabilized Classes Enforces Callback & lt; Boolean & gt; {@Override Public Boolean Call () {HttpURLConnection conn = null; {URL url = Try new URL ("http://www.myserver.com"); Conn = (HttpURLConnection) url.openConnection (); Conn.setConnectTimeout (5000); Conn.setReadTimeout (5000); Conn.setRequestMethod ("GET"); Int response = conn.getResponseCode (); Return reaction == 200; } Hold (IOException e) {e.printStackTrace (); return false; } Hold (exception e) {e.printStackTrace (); return false; }}}   

For some reason, the server does not respond or the server responds very slowly. Due to the behavior of this server, both connections are time-consuming and do not have time to read

So I want to monitor this work (thread is running in the pool executor), and the thread is something It is time to interrupt and ensure that I remove this work from the pool so that this work is not done in the thread pool and free.

2, 5, 5, time unit. SEODNS, new Arrawlocking Q & A; runnable & gt; (10)); Future & lt; Boolean & gt; F = tpe.submit (new MyRunnable ()); Try {Boolean breakthrough = F. Jacket (5000, Time UNNET; MILILXNDNS); } Hold (exception e) {e.printStackTrace (); }

from timeout exposure to f.get call after 5000 milliseconds if The thread that does not respond to MyRunnable work. But I can see that this work is going on in the thread pool provider, if it is stuck on HttpURLConnection.getResponseCode () .

When I tried f. Cancel (true) , this is not interfering with thread when the task HttpURLConnection.getResponseCode () method gets stuck in the call. I think the thread will be interrupted only when it will be testing it for the next available opportunities. So I think that future.cancel does not help in this case.

How do I remove this call (by talking to the thread pool executor) and make sure that the work is removed from the pool and the related thread is free to handle different tasks?

Thanks for the reply.

Thanks,

Raghu

I think that You can share the connection with another thread, which will call disconnect () on the deadline after it, so it should be something like this:

  Fixed class connection stapler applies Runnable {Private Final HttpURLConnection Conn; Connection Installer (HTTPPLRL Connection Connection) {If (Conne == blank) {New Nulloinction Exception (Throw); } This.conn = conn; } @ Override Public Wide Run () {conn.disconnect (); }} ... ScheduledExecutorService Service = Executor. NewScheduledThreadPool (THREAD_POOL_SIZE); ... // submit your own, submit, then a connectionstopper service. Schedule (New Connection Lock (Conn.), 5, Time Unit. SEODNSS);    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -