Created
December 1, 2016 06:50
-
-
Save prashanth-g/4881320c353b671feb7557b6a3cbc73d to your computer and use it in GitHub Desktop.
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
| import java.util.Map; | |
| import java.util.concurrent.ConcurrentHashMap; | |
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.TimeUnit; | |
| public class HelloWorld { | |
| static Map<String, AtomicLong> orders = new ConcurrentHashMap<>(); | |
| static void processOrders() { | |
| for (String city : orders.keySet()) { | |
| for(int i=0; i < 50; i++) { | |
| Long oldOrder = orders.get(city); | |
| orders.put(city, oldOrder+1); | |
| } | |
| } | |
| } | |
| public static void main(String[] args) throws InterruptedException { | |
| orders.put("Bombay", 0l); | |
| orders.put("Beijing", 0l); | |
| orders.put("London", 0l); | |
| orders.put("NewYork", 0l); | |
| ExecutorService service = Executors.newFixedThreadPool(2); | |
| service.submit(HelloWorld::processOrders); | |
| service.submit(HelloWorld::processOrders); | |
| service.awaitTermination(1, TimeUnit.SECONDS); | |
| service.shutdown(); | |
| System.out.println(orders); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment