Example of Concurrenthashmap in java--turn

Source: Internet
Author: User
Tags concurrentmodificationexception

Original address: Http://www.concretepage.com/java/example_concurrenthashmap_java

On the This page we'll provide example of Concurrenthashmap in Java. Concurrenthashmap is the thread safe but does does use the locking on the complete map. It is fast and have better performance in comparison to Hashtable in concurrent environment. Find some methods of ConcurrentHashMap .

get () : Pass The key as an argument and it would return associated value.
put (): Pass key and value and it'll map.
putifabsent (): Pass key and value and it would map only if key was not already present.
Remove (): Removes the entry for the given key.

Contents
    • Java Concurrenthashmap Internal Working
    • Concurrenthashmap Example
    • Concurrenthashmap vs Hashtable
    • Concurrenthashmap vs HashMap
Java Concurrenthashmap Internal Working

1.Concurrency for Retrieval: Retrieval of elements fromConcurrentHashMapdoes not use locking. IT may overlap with update operation. We get the elements of last successfully completed update operation. In case of aggregate operations such asputAllandclear(), concurrent retrieval may show insertion or removal of the only some elements.

2.Iteration of Concurrenthashmap: Iterators and enumerations also return the elements which has been concurrently added while iterating.ConcurrentHashMapdoes not throwConcurrentModificationException.

3.Concurrency for Updates: Concurrent updates is thread safe.ConcurrentHashMapConstructor has a optional concurrency level argument. The default value is 16. This is the estimated number of concurrently updating threads. It is used in internal sizing to accommodate concurrently updating threads. Hash table is internally partitioned to the concurrency level number so that it can avoid updating concurrent thread con Tention.

Concurrenthashmap Example

Find the example.
Concurrenthashmapdemo.java

PackageCom.Concretepage;ImportJava.Util.Iterator;ImportJava.Util.Concurrent.Concurrenthashmap;ImportJava.Util.Concurrent.Executorservice;ImportJava.Util.Concurrent.Executors;Public Class Concurrenthashmapdemo { Private Final Concurrenthashmap<Integer,String>Conhashmap= New Concurrenthashmap<Integer,String> (); Public Static voidMain(String[]Args) { ExecutorserviceService= Executors.Newfixedthreadpool(3); ConcurrenthashmapdemoOb= New Concurrenthashmapdemo();Service.Execute(Ob.New Writethreasone());Service.Execute(Ob.New Writethreastwo());Service.Execute(Ob.New Readthread());Service.Shutdownnow(); } Class Writethreasone Implements Runnable {@OverridePublic voidRun() {For(IntI= 1;I<=10;I++) {Conhashmap.Putifabsent(I, A+I);}} } Class Writethreastwo Implements Runnable {@OverridePublic voidRun() {For(IntI= 1;I<=5;I++) {Conhashmap.Put(I, B+I);}} } Class Readthread Implements Runnable {@OverridePublic voidRun() { Iterator<Integer>Ite=Conhashmap.KeySet().Iterator(); While(Ite.hasnext ()) { integer Key = Ite.system.. Println (key+ + Conhashmap.key}} } Span class= "pun" >               

Output

1 : B12 : B2 3 : B34  : B45: B56 :  A67 : A7 8 : A89  : A910: A10             
Concurrenthashmap vs Hashtable

1. Is ConcurrentHashMap based on hash table. It allows to put a null object but Hashtable allows only Not-null object.

2. The object which is being used as key must override hashCode() and equals() methods.
3. The methods such as get() , put() is remove() synchronized in Hashtable whereas does not use ConcurrentHashMap synchronized methods for C Oncurrency.

4. In concurrent modification, the iteration of Hashtable elements would throw the whereas does not ConcurrentModificationException ConcurrentHashMap .

Concurrenthashmap vs HashMap

1. ConcurrentHashMap and HashMap both is based on hash table.

2. ConcurrentHashMap supports full concurrency of retrieval. HashMap can is synchronized using Collections.synchronizedMap() .

3. ConcurrentHashMap provides concurrency level for updates the can is changed while instantiating.

4. In concurrent modification HashMap throws ConcurrentModificationException whereas ConcurrentHashMap does not.

Example of Concurrenthashmap in java--turn

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.