Java – Future.get() and InterruptedException Asynchronous threading

asynchronousfuturejavamultithreadingtimeout

Im using asynchronous threading in my application WITH httpClient. I make a call using the Future Api like so

mStrResults = (String) rssFuture.get();

this call attempts to retrieve an html string returned from my Callable httpClient call() method.

What i want to do however is ensure that the get method does not wait too long while executing the call() method. Should i pass a timeout parameter when calling rssFuture.get() or is it ok to just surround with a InterruptedException catch block?

Also is there a default time which the asynchronous thread will wait before throwing an InterruptedException and if so can i set a custom value?

Best Solution

You should pass a timeout parameter when calling rssFuture.get() and catch the TimeoutException. An InterruptedException will only happen if the thread running the your call gets interrupted with the Thread.interrupt method or if you call the cancel(true) method in the Future obj.