Skip to content

Instantly share code, notes, and snippets.

@ova2
Last active May 14, 2021 20:51
Show Gist options
  • Save ova2/fedcf0220b4b77a0af7a905358df1374 to your computer and use it in GitHub Desktop.
Save ova2/fedcf0220b4b77a0af7a905358df1374 to your computer and use it in GitHub Desktop.
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
class Test {
public static void main(String[] args) {
Supplier<Integer> task1 = () -> {
// execute a long-running task (just for demo purpose)
int result = 0;
for (int i = 0; i < 10000; i++) {
result = new Random().nextInt(100);
}
return result;
};
Supplier<List<Integer>> task2 = () -> {
// execute a long-running task (just for demo purpose)
List<Integer> result = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
result.add(new Random().nextInt(100));
}
return result;
};
LongRunningTaskService service = new LongRunningTaskService();
service.executeCompletableFuture(task1).thenAccept(System.out::println);
service.executeMono(task1).doOnNext(System.out::println).subscribe();
service.executeFlux(task2).take(3).doOnNext(System.out::println).subscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment