Created
December 1, 2016 06:50
-
-
Save prashanth-g/5772b66528a762b5081354e423132a45 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; | |
| import java.util.concurrent.atomic.AtomicLong; | |
| 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++) { | |
| orders.get(city).getAndIncrement(); | |
| } | |
| } | |
| } | |
| public static void main(String[] args) throws InterruptedException { | |
| orders.put("Bombay", new AtomicLong()); | |
| orders.put("Beijing", new AtomicLong()); | |
| orders.put("London", new AtomicLong()); | |
| orders.put("NewYork", new AtomicLong()); | |
| 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