This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import java.util.List; | |
| import java.util.concurrent.CompletableFuture; | |
| import java.util.stream.Collectors; | |
| public class Compare3aWrappers { | |
| // data structures | |
| class RSS { | |
| String getFeedAddress() { return null; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test | |
| import scala.concurrent.Future | |
| class Compare3bWrappers { | |
| // data structures | |
| case class Rss(feedAddress: String) | |
| // I/O operations: non-blocking, asynchronous | |
| def fetchFromDb[T](entityClass: Class[T], id: Long): Future[T] = ??? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| public class Compare4Synchronous { | |
| // data structures | |
| public class ProfileData { | |
| public long getUserId() { return 42; } | |
| } | |
| public class User { | |
| public String getEmail() { return null; } | |
| boolean getNotificationsEnabled() { return false; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import java.util.concurrent.CompletableFuture; | |
| public class Compare4Wrappers { | |
| // data structures | |
| public class ProfileData { | |
| public long getUserId() { return 42; } | |
| } | |
| public class User { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test | |
| import kotlinx.coroutines.experimental.Deferred | |
| import kotlinx.coroutines.experimental.async | |
| import test.Compare4Synchronous.ProfileData | |
| import test.Compare4Synchronous.User | |
| import kotlin.reflect.KClass | |
| fun compare4() { | |
| // I/O operations: non-blocking, asynchronous |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import java.util.concurrent.CompletableFuture; | |
| import java.util.function.Consumer; | |
| // implementations are omitted | |
| class User { | |
| String getProfileId() { return null; } | |
| String getEmail() { return null; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Synchronous { | |
| // I/0 operations: blocking, synchronous | |
| <T> T fetchFromDb(Class<T> entityClass, long id) { return null; } | |
| String sendHttpGet(String url) { return null; } | |
| void sendEmail(String to, String content) { } | |
| // the business logic: synchronous | |
| void runBusinessProcess() { | |
| User user = fetchFromDb(User.class, 42); | |
| String profile = sendHttpGet( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Callbacks { | |
| // I/O operations: non-blocking, asynchronous | |
| <T> void fetchFromDb(Class<T> entityClass, long id, Consumer<T> callback) { } | |
| void sendHttpGet(String url, Consumer<String> callback) { } | |
| void sendEmail(String to, String content, Runnable callback) { } | |
| // the bussines logic: asynchronous | |
| void runBusinessProcess(Runnable callback) { | |
| fetchFromDb(User.class, 42, | |
| // continuation: what should happen after the user is read from the db |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Wrappers { | |
| // I/O operations: non-blocking, asynchronous | |
| <T> CompletableFuture<T> fetchFromDb(Class<T> entityClass, long id) { return null; } | |
| CompletableFuture<String> sendHttpGet(String url) { return null; } | |
| CompletableFuture<Void> sendEmail(String to, String content) { return null; } | |
| // the bussines logic: asynchronous | |
| CompletableFuture<Void> runBusinessProcess() { | |
| // thenCompose: also known as flatMap, combines two futures | |
| return fetchFromDb(User.class, 42).thenCompose(user -> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| case class RateLimiterQueue[F](maxRuns: Int, perMillis: Long, | |
| lastTimestamps: Queue[Long], waiting: Queue[F], scheduled: Boolean) |