Je suis nouveau sur RxJava, et j'ai besoin d'utiliser la fonction Observable de manière asynchrone.Timeout dans RxJava
Je dois également utiliser des délais d'attente: dans mon exemple, je veux que chaque processus se termine en 1 seconde ou moins.
Voici ce que je l'ai fait pour l'instant:
public static void hello(String name) throws IOException {
Observable<String> obs2 = Observable.just(name).timeout(1000, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io());
obs2.subscribe(new Action1<String>() {
@Override
public void call(String s) {
if("CCCCC".equals(s)){
try {
Thread.sleep(3200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(s + " " + new Date() +" "+Thread.currentThread().getName());
}
});
}
public static void main(final String[] args) throws InterruptedException, IOException {
hello("AAAAA");
hello("CCCCC");
hello("BBBBBB");
System.in.read();
}
Résultat:
AAAAA Thu Oct 05 09:43:46 CEST 2017 RxIoScheduler-2
BBBBBB Thu Oct 05 09:43:46 CEST 2017 RxIoScheduler-4
CCCCC Thu Oct 05 09:43:49 CEST 2017 RxIoScheduler-3
Je me attendais en fait d'obtenir un TimeoutException du fil nommé "RxIoScheduler-3", car il a dormait depuis 3 secondes.
Quel est le problème avec mon code et mon approche des délais d'attente dans RxJava?
Merci de votre aide.
vous remercie pour votre aide, il fonctionne bien! – tlebras