Great Deals

ConcurrentHashMap in nutshell

19:25 Unknown 0 Comments

The Java concurrent hash map may be considered as the lodge having many rooms with locking facility.Where as hash map may be considered as the house with one door.

So in case of hash map if you are locking the door. Then you will not be able to access if any of the room of house. Before using hash map every thread locks the hash map making it inaccessible to other thread till the  time it won't complete it's operations. 
Hashmap is like your house
Hashmap

But if concurrent hash map is viewed it may be viewed as multiple separate rooms of Lodge if any room is locked we can still access the other rooms.

Concurrent hashmap used by multiple thread

Creation of ConcurrentHashMap


public ConcurrentHashMap(int initialCapacity,float loadFactor, int concurrencyLevel) 

HashMap anatomy:



Initial Capacity: 

You can specify the size of the concurrenthashmap at the time of creation.


 static final int DEFAULT_INITIAL_CAPACITY = 16;

Load Factor: 

Tolerable capacity of concurrenthashmap if it exceeds concurrenthashmap needs to be resized. However resizing operation is relatively slow.

 static final float DEFAULT_LOAD_FACTOR = 0.75f;

ConcurrencyLevel:

Estimated number of concurrently updated threads.

Advantages of concurrenthashmap:


ConcurrentHashMap can be used to make the concurrent operations possible without waiting for other threads job completion

Increased throughput.

For Producer Consumer Example on concurrenthashmap Producer Consumer in ConcurrentHashmap

Disadvantages of concurrency in hashmap


           Methods including size, isEmpty, and containsValue are typically useful only when a map is not undergoing concurrent updates in other threads. Otherwise these methods procure precise results.

When resizing is done?

1.       
       When the concurrenthashmap is almost full 75% of its defined capacity.
2.       When collisions are increased so to avoid collisions concurrenthashmap  is resized.

Properties of ConcurrentHashmap

1.      

         ConcurrentHashmap does not allow null value.
2.       ConcurrentHashMap does not allow duplicate keys.

public class CHashMap {

            public static void main(String[] args) {
ConcurrentHashMap<String, String> ch=new ConcurrentHashMap<String, String>();
ch.put("a", "a");
ch.put("a", "a");
Enumeration<String> enum1=ch.keys();

while(enum1.hasMoreElements())
{
            String key=enum1.nextElement();
            System.out.println("Key is ==>"+key+" Value is ===>"+ch.get(key));
            }
            }

}

Output is: Key is ==>a Value is ===>a


0 comments:

Java Helloworld

18:35 Unknown 0 Comments

Calcuator Demo
package com.oop;
class Calculator
{
     public Calculator() {
          System.out.println("I am constructor");
         
     }
     //1.add 2.sub 3.multiplication 4.division
     public int add(int num1,int num2)
     {
         
          return (num1+num2);
     }
}
public class CalculatorDemo {

     public static void main(String[] args)
     {
          Calculator calcObj=new Calculator();
          System.out.println("addition is "+calcObj.add(2, 3));
         
     }
}

Example
package com.oop;
class Human
{
     private String address;
    private int age;
     public int getAge() {
          return age;
     }

     public void setAge(int age) {
          this.age = age;
     }

     public String getAddress() {
          return address;
     }
  
     public void setAddress(String address) {
          this.address = address;
     }
    
}
public class HelloWorld {
     //Type2:declaration and oneliner init
     //datatype varname="value"
static String empName="Ashwin More";


//Type1: declaration and then intialization
//dataType variable Name;
//variableName="value";

//creation of objects
//ClassName objectName=new className();


public static void main(String[] args) {
     System.out.println("HI Java Batch and i am "+empName);
    
     Human objectHuman=new Human();
     objectHuman.setAddress("Pune");
     objectHuman.setAge(23);
    
     Human objectHuman1=new Human();
     objectHuman1.setAddress("Pune");
     objectHuman1.setAge(20);
    
     System.out.println(""+objectHuman1.getAddress());
}   
    
}
//visibily modifier class ClassName

0 comments:

Advertising