Skip to content

Instantly share code, notes, and snippets.

@prashanth-g
Created December 1, 2016 06:50
Show Gist options
  • Select an option

  • Save prashanth-g/5772b66528a762b5081354e423132a45 to your computer and use it in GitHub Desktop.

Select an option

Save prashanth-g/5772b66528a762b5081354e423132a45 to your computer and use it in GitHub Desktop.
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