Skip to content

Instantly share code, notes, and snippets.

@Sloy
Created May 4, 2020 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sloy/01d79e0483607307a733783213caafd5 to your computer and use it in GitHub Desktop.
Save Sloy/01d79e0483607307a733783213caafd5 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class SyncAsync {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(() -> {
System.out.println("HELLO");
}).start();
}
System.out.println("WORLD");
}
}
/** @noinspection ALL */
class Futures {
public static void main(String[] args) {
// supplyAsync
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
sleep(1000);
System.out.println("Background thread");
return "2";
});
future
.thenCompose((String result) -> processValue(result))
.thenAccept((Integer result) -> {
System.out.println(result);
});
System.out.println("Main thread");
sleep(3000);
}
private static CompletableFuture<Integer> processValue(String value) {
return CompletableFuture.supplyAsync(() -> {
sleep(1000);
return Integer.parseInt(value);
});
}
public static void sleep(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException ignored) {
}
}
}
class Lists {
public static void main(String[] args) {
// CompletableFuture<T> - Task<T>
// thenCompose - continueWithTask
// thenApply - continueWith
// thenAccept - addOnSuccessListener
// whenAllSuccess
// 1. Query for profile
getProfile("rafa")
.thenCompose((List<Integer> escuelas) -> {
// 2. Launch one query for each school
List<CompletableFuture<String>> allTasks = new ArrayList<>();
for (Integer escuelaId : escuelas) {
allTasks.add(getEscuela(escuelaId));
}
// 3. Combine all queries into one
return Firebase.whenAll(allTasks);
})
// 4. Obtain result of the combined queries
.thenAccept((escuelas) -> {
setAdapterInfo(escuelas);
});
sleep(3000);
}
private static void setAdapterInfo(List<String> escuelas) {
System.out.println(escuelas);
}
private static CompletableFuture<List<Integer>> getProfile(String userId) {
return CompletableFuture.supplyAsync(() -> Stream.of(1, 2, 3, 4, 5, 6).collect(Collectors.toList()));
}
private static CompletableFuture<String> getEscuela(Integer escuelaId) {
return CompletableFuture.supplyAsync(() -> {
sleep(1000);
return escuelaId.toString();
});
}
public static void sleep(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException ignored) {
}
}
}
class Firebase {
public static <T> CompletableFuture<List<T>> whenAll(List<CompletableFuture<T>> tasks) {
CompletableFuture<Void> allFutures = CompletableFuture.allOf(tasks.toArray(new CompletableFuture[0]));
return allFutures.thenApply(v ->
tasks.stream().
map(CompletableFuture::join).
collect(Collectors.toList()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment