Domanda

Sto usando il threading asincrono nella mia applicazione WITH httpClient. Faccio una chiamata usando Future Api in questo modo

mStrResults = (String) rssFuture.get();

questa chiamata tenta di recuperare una stringa html restituita dal mio metodo call () Callable httpClient.

Quello che voglio fare, tuttavia, è assicurarmi che il metodo get non aspetti troppo a lungo durante l'esecuzione del metodo call (). Devo passare un parametro di timeout quando chiamo rssFuture.get () o è giusto circondare con un blocco catch InterruptedException?

Esiste anche un tempo predefinito che il thread asincrono attenderà prima di lanciare una InterruptedException e, in tal caso, posso impostare un valore personalizzato?

È stato utile?

Soluzione

Dovresti passare un parametro di timeout quando chiami rssFuture.get() e intercettare TimeoutException. Una InterruptedException si verificherà solo se il thread che esegue il tuo call viene interrotto con il metodo Thread.interrupt o se chiami il metodo cancel(true) in Future obj.

Altri suggerimenti

Dovresti usare Future. get (long timeout, unità TimeUnit) e cattura TimeoutException. Non esiste un timeout predefinito per get (), attenderà per sempre .

InterruptedException non verrà generato a meno che il thread che chiama Future.get () non venga interrotto.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top