views:

356

answers:

2

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?

+1  A: 

You should use Future.get(long timeout, TimeUnit unit), and catch TimeoutException. There is no default timeout for get(), it will wait forever.

InterruptedException will not be thrown unless the thread calling Future.get() is interrupted.

refrus
+3  A: 

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.

bruno conde