Skip to content

Instantly share code, notes, and snippets.

@mzakyalvan
Last active August 15, 2018 04:05
Show Gist options
  • Select an option

  • Save mzakyalvan/7fe5f0cebea3a9038a2326dc7d4b7e2d to your computer and use it in GitHub Desktop.

Select an option

Save mzakyalvan/7fe5f0cebea3a9038a2326dc7d4b7e2d to your computer and use it in GitHub Desktop.
package sample;
import io.reactivex.Single;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class SimulateTimeout {
public static void main(String[] args) throws Exception {
SimulateTimeout sample = new SimulateTimeout();
CountDownLatch latch = new CountDownLatch(10);
for(int i = 0; i < 10; i++) {
sample.process("input#"+i)
.doOnSuccess(output -> latch.countDown())
.subscribe(output -> System.out.println(output), Throwable::printStackTrace);
}
latch.await();
}
final Random random = new Random();
public Single<String> process(String input) {
return Single.just(input)
.flatMap(request -> Single.just(request + " : Processed")
// Simulate long running process.
.delay(random.nextInt(2_000), TimeUnit.MILLISECONDS)
.timeout(1_000, TimeUnit.MILLISECONDS, Single.just(request + " : TimedOut")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment