Skip to content

Instantly share code, notes, and snippets.

@ranraj
Last active July 23, 2020 07:51
Show Gist options
  • Save ranraj/6c95d5d503f0c59246a227191845a5b9 to your computer and use it in GitHub Desktop.
Save ranraj/6c95d5d503f0c59246a227191845a5b9 to your computer and use it in GitHub Desktop.
Reactive
List<String> itemList = Arrays.asList("blue-cone", "red-cone", "gray-cone");
		ExecutorService service = Executors.newFixedThreadPool(5);
		itemList.forEach( item -> {
			
			Future<String> result = service.submit(() -> {
				//IO operation
				Thread.sleep(2000);
				return item;
			});
			try {
				System.out.println(result.get()); // blocking operation
			} catch (InterruptedException | ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
		System.out.println("Order dispatched");
public class IceCreamParlour {
	static final Logger logger = Logger.getLogger(IceCreamParlour.class.getSimpleName());
	List<String> itemList = Arrays.asList("blue-cone", "red-cone", "gray-cone");
//	List<String> itemList = Arrays.asList("blue-cone", "red-cone", "gray-cone", "blue-cone1", "red-cone1", "gray-cone1",
//			"blue-cone2", "red-cone2", "gray-cone2", "blue-cone3", "red-cone3", "gray-cone3", "blue-cone4", "red-cone4",
//			"gray-cone4");

	Iterator<String> itemIterator = itemList.iterator();

	String pickCone() {
		return itemIterator.next();
	}

	String flavour(String iceCream) {
		return "Vannila\n" + iceCream;
	}

	String decorate(String supply) {
		return "Topings\n" + supply;
	}

	String chacoDip() {
		return itemIterator.next();
	}

	String wrapper(String s) {
		return "[" + s + "]";
	}

	void deliver(String s) {
		logger.info(s);
	}

	boolean hasNextOrder() {
		return itemIterator.hasNext();
	}
}
public class IceCreamParlourApplication {
	static final Logger logger = Logger.getLogger(IceCreamParlourApplication.class.getSimpleName());

	public static void main(String[] args) {

		IceCreamParlour iceCreamPourler = new IceCreamParlour();
		while (iceCreamPourler.hasNextOrder()) {

			CompletableFuture.supplyAsync(iceCreamPourler::pickCone).thenApply(iceCreamPourler::flavour)
					.thenApply(iceCreamPourler::decorate).thenApply(iceCreamPourler::wrapper)
					.thenAccept(iceCreamPourler::deliver);
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment