Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8126323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:06:40+00:00 2026-06-06T07:06:40+00:00

I was going through an interview question on JavaRevisited and I’m having difficulty understanding

  • 0

I was going through an interview question on JavaRevisited and I’m having difficulty understanding this question :

What’s wrong with using a HashMap in a multithreaded environment? When get() method go into an infinite loop?

In my opinion, it’s not a problem to use HashMap inside a multi-threaded environment, as long as our application is not accessing/reading threads which are modifying the created HashMap, rather than simply accessing the HashMap.

So, as I see it, there’s not a problem as long as in the application we are just accessing the HashMap in a multi-threaded environment.

Please let me know if my understanding is correct.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:06:41+00:00Added an answer on June 6, 2026 at 7:06 am

    What’s wrong using HashMap in multithreaded environment? When get() method go to infinite loop?

    It is a bug to have multiple threads use a non-synchronized collection (really any mutable class) in an unprotected manner. Certain if each thread had their own HashMap instance then this is not an issue. It is a problem if multiple threads are adding to the same HashMap instance without it being synchronized. Even if just 1 thread is modifying a HashMap and other threads are reading from that same map without synchronization, you will run into problems.

    If you need to use the same hash table object in multiple threads then you should consider using ConcurrentHashMap, wrapping each of the accesses to the HashMap in a synchronized {} block, or making use of the Collections.synchronizedMap(new HashMap<...>()) construct.

    Chances are that the get() goes to an infinite loop because one of the threads has only a partially updated view of the HashMap in memory and there must be some sort of object reference loop. That’s the peril of using an unsynchronized collection with multiple threads.

    So in my understanding, it’s not a problem as long as in the application we are just accessing the HashMap in a multi-threaded environment?

    If by "accessing" you mean "reading", then this is true with qualifications. You must make sure:

    • All of the updates to the HashMap are completed before the threads are instantiated and the thread that creates the map also forks the threads
    • The threads are only using the HashMap in read-only mode – either get() or iteration without remove
    • There are no threads updating the map

    If any of these conditions are not true then you will need to use a synchronized map instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was going through some interview question on linux i found What would happen
While going through Qt code I had this basic question on the pimpl implementation.
Im going through a log file using php and it looks either like this:
I was just going through certain interview questions. Got this structure related issue, I
While going through many resources on multithreaded programming, reference to volatile specifier usually comes
I was going through some Amazon interview questions on CareerCup.com, and I came across
Going through my code, I was hoping to remove instances where I do this:
Going through this article class ThreadTest { static void Main() { Thread t =
So I was going through some interview questions and I came across one about
while going though sql server interview question in book of mr. shiv prashad koirala.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.