Pavitar has posted 1 posts at DZone. View Full User Profile

Concurrency and HashMap

06.30.2008
| 23276 views |
  • submit to reddit

In theory everyone knows Hash Map is not Thread Safe and it shouldn’t be used in multi Threaded applications. But still people come out with their own theories that they can use HashMap in their context. Some say they are just reading the data and map is not written to a lot. Unfortunately none of these explanations holds good when one lands up in a synchronization issue. Normally most of the guys do not understand fundamentals around Java Memory Model and Concurrency .One cannot blame them for not knowing their fundamentals as its hard for people to visualize concurrent executions since from college days we are used to sequential executions of program.

Enuf of blame game, now lets look into some code.Have a look at the code mentioned below and come out with all your theories of what can go wrong with it or theories which say its all correct.

public class MapTestTask implements Runnable {

private Map hashMap;

private Object      value = new Object();

public MapTestTask(Map map) {
  this.hashMap = map;
}

public void run() {
  hashMap.put(Thread.currentThread(), value);
  Object retrieved = hashMap.get(Thread.currentThread());
   if (retrieved == null) {
  // Can it ever Happen
  }
}
}

Now question is when we run multiple such Threads can we ever see retrieved as null.

If we look from sequential point of view it can never happen.But when concurrency comes into picture this can happen. I will give you a code with which can reproduce this scenario.

This is my sincere advise : do not over engineer when Concurreny is involved. Because none of the theories will stand the test of time in a concurrent environment. As a rule of thumb use Thread safe collections wherever concurrency is involved.

Finally i will leave you with this interesting bug in Java which says HashMap can get into infinite loop when used in muti Threaded Environment which further reiterates my point that not all the possible scenarios can be visualized in a multi threaded environment and therefore one should rely on basics rather than trying a smart creativity which has high probability of landing into trouble at some point in future.
You can read details about the infinite loop problem here.

Source Code for Reproducing

package test;

import java.util.Map;

public class MapTestTask implements Runnable {

    private Map hashMap;

    private Object      value = new Object();

    public MapTestTask(Map map) {
        this.hashMap = map;
    }

    public void run() {
                hashMap.put(Thread.currentThread(), value);
                Object retrieved = hashMap.get(Thread.currentThread());
                if (retrieved == null) {
                    // Can it ever Happen
                    System.out.println("Oh My God it can happen.");
                }

            }
}
package test;

import java.util.Map;
import java.util.HashMap;

public class TestMap {

    public static void main(String[] args) throws InterruptedException {

        Map map = new HashMap();

        int NUM_THREADS = 1000;
        Thread[] threads = new Thread[NUM_THREADS];
        for (int i = 0; i < NUM_THREADS; i++) {
            threads[i] = new Thread(new MapTestTask(map));
        }

        for (int i = 0; i < NUM_THREADS; i++) {
            threads[i].start();
        }

        for (int i = 0; i < NUM_THREADS; i++) {
            threads[i].join();
        }
    }

}

From http://pitfalls.wordpress.com/2008/06/29/concurrencyandhashmap/

Published at DZone with permission of its author, Pavitar Singh.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

David Voo replied on Mon, 2008/06/30 - 3:35am

Nice tutorial. Maybe sometimes KISS is the perfect methodology when coming to programming

Tranquiliser Gt replied on Mon, 2008/06/30 - 5:00am

Good pick up. It took me a while to figure out why this ThreadLocal like code could fail. Since hashmap internally maintains a limited (not fixed) number of buckets, there exists a chance of having some values inserted to the same bucket. Unfortunately, a bucket is a linked list and not thread safe so that multiple put may fail.

As experiment, I increased the initial capacity of the hash map to 10000. Not surprisely, the chance of failure is greatly reduced. Now I'm interested to know, if possible, whether reimplementing the bucket with a thread-safe linked list such as atomic integer based would make the hashmap thread-safe.

 

 

 

 

 

 

Artur Biesiadowski replied on Mon, 2008/06/30 - 8:25am

[quote=tranquiliser]

Now I'm interested to know, if possible, whether reimplementing the bucket with a thread-safe linked list such as atomic integer based would make the hashmap thread-safe.[/quote]

When adding entries to the map, you have a chance for extending the map and causing full rehash. Anybody accessing/modifying the map at this moment would get/set corrupted data again.

Look for ConcurrentHashMap from JDK or FastMap from Javolution for examples how it can be done safely without too much contention. If you feel really, really brave, check out ConcurrentSkipListMap from JDK.

Slava Imeshev replied on Mon, 2008/06/30 - 5:10pm

 

"In theory everyone knows Hash Map is not Thread Safe and it shouldn’t be used in multi Threaded applications"

This is a false as it ever gets. There is nothing wrong about using HashMap in a multithreaded application. What can be wrong is an improper use or lack of synchronization,  but that's not HashMap's fault.

Regards,

 

Slava Imeshev

 

Peter Lawrey replied on Wed, 2008/07/02 - 12:58am in response to: Tranquiliser Gt

I think its a bit more compilcated than this.

You can use ConcurrentHashmap, so do so. 

If you are wondering why, have a look at its code and see what it does for you. 

jame jack replied on Wed, 2009/06/17 - 7:51pm

تحميل برامج برامج جوالات العاب بنات تكنولوجيا كتب تعليم UltraSurf Internet Download Manager ProgDVB برامج مجانية أفضل المواقع العربية مشاهدة محطات مشفرة Online TV Player 3.0.0.940 Internet Download Manager 5.17 Build 4 رقص شرقي anyTV Pro 4.32 OnLineLive 7.1.1 هزي يانواعم ProgDVB 6.06.2 SopCast 3.0.3 Falco Image Studio 3.6 لعبة تزلج على الجليد UltraSurf 9.4 كاثرين هيغل Katherine Heigl محطة غنوة FreeZ Online TV 1.0 Free Video to Mp3 Converter 3.1.3.51 Advanced MP3 Converter 2.10 Xilisoft Video to Audio Converter 5.1.23.0515 Blaze Media Pro 8.02 AKRAM Media Creator 1.11 DVD Audio Extractor 4.5.4 Free WMA to MP3 Converter 1.16 لعبة نينجا المتقدم لعبة قذف كرة لعبة دراجات البهلوانية لعبة اعداء الغابة تحميل برامج Download DivX Subtitles 2.0 BullGuard 8.5 Google Chrome 2.0.181.1 Dev Dell Studio XPS Desktop 435T Intel Matrix Storage Manager A00 Gigabyte GA-EP45-UD3P Bios F9 Ambush HDConvertToX 1.1.229.1764 MSI Wind Nettop CS 120 Realtek Audio Driver 5.10.0.5618 Biostar T41-A7 6.x Realtek On-Board Audio Driver 5.10.0.5735 for 2000/2003/XP TweakNow RegCleaner 4.1.1 SpeedItup Free 4.97 برامج العاب - Internet Download Manager - برامج جوالات - العاب - محطة غنوة - قنوات فضائية - بنات - تكنولوجيا - كتب تعليم - UltraSurf -

Ahmed Hashem replied on Fri, 2014/08/01 - 9:11am


nice

العاب طبخ بنات 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.