1. You can change HashMap to ConcurrentHashMap. ConcurrentHashMap is designed to be thread-safe, so it operates stably in a multi-threaded environment and has slightly better performance than Hashtable and synchronized map. ConcurrentHashMap locks the map when you put it, so you can prevent the same key put at the same time.

  2. Open the file in Cloud9. cloud901

  3. Change the HashMap below to ConcurrentHashMap.

    +   import java.util.concurrent.*;
    -   private HashMap<Integer, Concurrency> map = new HashMap<>();
    +   private ConcurrentHashMap<Integer, Concurrency> map = new ConcurrentHashMap<>();
  4. The final source code

    package com.example.concurrencysample;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.concurrent.*;
    import com.amazonaws.auth.AWSCredentials;
    import com.amazonaws.auth.AWSStaticCredentialsProvider;
    import com.amazonaws.auth.BasicAWSCredentials;
    public class SingletonRepo {
        private static String ACCESS_KEY = "";
        private static String SECERET_KEY = "";
        private ConcurrentHashMap<Integer, Concurrency> map = new ConcurrentHashMap<>();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        private static SingletonRepo singletonrepo = null;
        static AWSCredentials ac;
        static AmazonDynamoDB dynamodb;
        private SingletonRepo() {
        public synchronized static SingletonRepo getInstance() {
            if (singletonrepo == null) {
                singletonrepo = new SingletonRepo();
                ac = new BasicAWSCredentials(ACCESS_KEY, SECERET_KEY);
                dynamodb = AmazonDynamoDBClientBuilder
                .withCredentials(new AWSStaticCredentialsProvider(ac) )
                System.out.println("singleton repo instance created");
            return singletonrepo;
        public Concurrency get(int product_code){
            return map.get(product_code);
        public Concurrency putKey(int product_code ) {           
            Date date = Util.getRandomDate();
            if (map.containsKey(product_code)) {
                return map.get(product_code);
                return map.put(product_code, new Concurrency(product_code,"test" , date));
        public void delKey(int product_code ) { 
            if (map.containsKey(product_code)) {
        public int getMapCount() {
            return map.size();
        public synchronized void clearHashMap() {
        public Concurrency getKey(int product_number) {
            return map.get(product_number);  
  5. push the changes again.

    git add .
    git commit -m "fix"
    git push
  6. And if you check the concurrencysample of Build, you can see that the build was successful cloud901

  7. let’s also check the Report. If you select Concurrency-Unittest-Report in Report group of Build, you can see the result passed by 100%. cloud901

  8. it passed all tests, it was automatically deploy to the dev environment according to Pipeline’s process. As such, unittest is very useful for quickly testing software. You can use the CodeBuild Report to quickly visualize the results of unit tests to help fix bug.

  9. Let`s go to CodePipeline :

    Select Pipeline in left menu and click concurrencysample-develop. this pipeline triggerd committing develop branch in concurrencysamle repository. you can find recently successed execution result. cloud901

  10. binary deployed to staging environment EC2 while pipeline all stage and job, test sucessed. cloud901 if you useing pipeline, you can configure automatic each stage for build, test, deploy enviroment. this help to more fast devlop cycle, and reduce hand on error. you can find more detail info click each staging.

Now let`s create a pull-request for deployment code to production environment