DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mastering SwiftUI Gestures: Basic to Advanced
  • RAG Done Right: When to Use SQL, Search, and Vector Retrieval and How To Combine Them
  • Why Your RAG Pipeline Will Fail Without an MCP Server
  • Designing Self-Healing AI Infrastructure: The Role of Autonomous Recovery

Trending

  • Dear Micromanager: Your Distrust Has a Job; It’s Just Not the One You’re Doing
  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • LLM Integration in Enterprise Applications: A Practical Guide
  • Understanding MCP Architecture: LLM + API vs Model Context Protocol
  1. DZone
  2. Data Engineering
  3. Data
  4. Concurrency and HashMap

Concurrency and HashMap

By 
Pavitar Singh user avatar
Pavitar Singh
·
Jun. 30, 08 · Interview
Likes (0)
Comment
Save
Tweet
Share
37.8K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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/

Data structure

Opinions expressed by DZone contributors are their own.

Related

  • Mastering SwiftUI Gestures: Basic to Advanced
  • RAG Done Right: When to Use SQL, Search, and Vector Retrieval and How To Combine Them
  • Why Your RAG Pipeline Will Fail Without an MCP Server
  • Designing Self-Healing AI Infrastructure: The Role of Autonomous Recovery

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook