Skip to content

Instantly share code, notes, and snippets.

@Jotschi
Created July 12, 2015 14:40
Show Gist options
  • Save Jotschi/c94cf7396bb698194e60 to your computer and use it in GitHub Desktop.
Save Jotschi/c94cf7396bb698194e60 to your computer and use it in GitHub Desktop.
import static io.vertx.core.Vertx.vertx;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class AsyncTest {
@Test
public void testProducts() {
// Create some products
List<Product> products = new ArrayList<>();
for (int i = 0; i < 10; i++) {
products.add(new Product("Test_" + i));
}
// Iterate over the products and transform them asynchronously
List<String> transformed = new ArrayList<>();
for (Product product : products) {
product.transform(rh -> {
transformed.add(rh.result());
});
}
// Check the transformed list.
System.out.println(transformed.size());
}
}
class Product {
String name;
public Product(String name) {
this.name = name;
}
Product transform(Handler<AsyncResult<String>> handler) {
vertx().executeBlocking(bc -> {
try {
Thread.sleep(100);
} catch (Exception e) {
}
bc.complete(name);
}, handler);
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment