Concurrent HashMap example:

Concurrent HashMap works on the principle of concurrent access of the segment of hashmap.
It partially locks the map which gives better performance.
See the simple example where producer keeps adding the element.


 Producing the Element and pushing it in hashmap
 public void addValue()
            {
                        for(int i=0;i<100;i++)
                        {
                                   
                                    Integer intObj=new Integer(i);
                                    System.out.println(Thread.currentThread().getName()+“Producing “+intObj);
                                    this.st.put(intObjintObj);
                        }

            }

Producer Code:


import java.util.ArrayList;
import java.util.List;
importjava.util.concurrent.ConcurrentHashMap;
class Producer implements Runnable
{
            voidinit(ConcurrentHashMap<Integer,Integer> st)
            {
                        this.st=st;
            }
            ConcurrentHashMap<Integer,Integer> st;
            public void addValue()
            {
                        for(int i=0;i<100;i++)
                        {
                                   
                                    Integer intObj=new Integer(i);
                                    System.out.println(Thread.currentThread().getName()+“Producing “+intObj);
                                    this.st.put(intObj, intObj);
                        }
            }
           
            @Override
            public void run() {
                       
                        addValue();
            }
           
}

Consumer


Consumer part:
Consumer keeps on utilizing whatever is present in the hashmap. While producing or retrieving we did not encounter ConcurrentModificationException  example.
How to create Consumer code we will use get() that will be called by Thread Object.

   public void getValue()
            {
                       
                        for(int i=0;i<100;i++)
                        {
                                   
                                    System.out.println(Thread.currentThread().getName()+ “Consuming “+this.st.get(i));
                        }

            }


class Consumer implements Runnable

{
            voidinit(ConcurrentHashMap<Integer,Integer> st)
            {
                        this.st=st;
            }
            ConcurrentHashMap<Integer,Integer> st;
           
            public void getValue()
            {
                       
                        for(int i=0;i<100;i++)
                        {
                                   
                                    System.out.println(Thread.currentThread().getName()+ “Consuming “+this.st.get(i));
                        }
            }
            @Override
            public void run() {
                       
                        getValue();
            }
           
}

Test Main:


public class HelloWorld{
     public static void main(String []args){
             ConcurrentHashMap<Integer,Integer> st=newConcurrentHashMap<Integer, Integer>();
             Producer hashObj=new Producer();
             hashObj.init(st);
             Thread t2=new Thread(hashObj);
             t2.setName(“t2  “);
         t2.start();
         Thread t3=new Thread(hashObj);
         t3.start();
         t2.setName(“t3  “);
         try {
                                    t2.join();
                        } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                        }
         try {
                                    t3.join();
                        } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                        }
         Consumer consumeObj=new Consumer();
         consumeObj.init(st);
       Thread t1=new Thread(consumeObj);
       t1.setName(“t1  “);
       t1.start();
      
     }
 }
Thank you for being kind enough to visit here.
If you really like this post you can boost my energy bar by pressing  Like Share 🙂

8 thoughts on “Concurrenthashmap Example”

  1. Hello just wanted to give you a quick heads up and let you know a few of the images aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same results.

  2. Hello there, simply became aware of your weblog through Google, and located that it is truly informative. I am gonna watch out for brussels. I’ll be grateful if you proceed this in future. A lot of other people will probably be benefited from your writing. Cheers!

  3. Way cool! Some extremely valid points! I appreciate you penning this post and the rest of the site is also really good.

  4. Hi! I know this is kinda off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having problems finding one? Thanks a lot!

  5. Hi there, I do believe your website may be having web browser compatibility problems. Whenever I look at your web site in Safari, it looks fine however, when opening in Internet Explorer, it’s got some overlapping issues. I simply wanted to give you a quick heads up! Aside from that, wonderful website!

Leave a Reply

Your email address will not be published. Required fields are marked *