Great Deals

Concurrenthashmap Example

13:03 suresh chaudhari 0 Comments

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;
import java.util.concurrent.ConcurrentHashMap;
class Producer implements Runnable
{
            void init(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
{
            void init(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=new ConcurrentHashMap<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 :)



0 comments:

Advertising