This file contains 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
public class Main { | |
public static void main (String[] args) { | |
Stomach stomach = new Stomach (); | |
long startTime = System.currentTimeMillis (); | |
for (int i = 0; i < 10000; i++) { | |
stomach.accept (new Salad ()); | |
stomach.accept (new Burger ()); | |
} | |
long normalDigestionDuration = System.currentTimeMillis () - startTime; | |
//------------------------------------------------------------------------------------------ | |
Consumer<Food> c = (Food food)-> { | |
System.out.println ("Eating the " +food); | |
}; | |
List<Food> foods = new ArrayList<> (); | |
for (int i = 0; i < 10000; i++) { | |
stomach.accept (new Salad ()); | |
stomach.accept (new Burger ()); | |
} | |
startTime = System.currentTimeMillis (); | |
foods.parallelStream ().map (food->{consumeFood (food,c);return food;}).count (); | |
long paralelStreamDigestDuration = System.currentTimeMillis () - startTime; | |
//------------------------------------------------------------------------------------------ | |
System.out.println ("Digestion duration is :" + normalDigestionDuration);// ~120 ms | |
System.out.println ("Digestion with parallel stream = " + paralelStreamDigestDuration);// ~7 ms...so fast huh ? | |
} | |
public static void consumeFood (Food food, Consumer<Food> consumer){ | |
consumer.accept (food); | |
} | |
} | |
Outputs:: | |
Digestion duration is :120 | |
Digestion with parallel stream = 7 |
No comments:
Post a Comment