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

  • SEARCH
  • Home
  • 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 9240653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:13:39+00:00 2026-06-18T08:13:39+00:00

I ran into some shady piece of code in which a HashSet is accessed

  • 0

I ran into some shady piece of code in which a HashSet is accessed and modified by several threads at the same time, and I wanted to know what effects this could have had, considering the Javadoc only explains the behavior is “non-deterministic” under concurrent, unsynchronized accesses.

Specifically, is it possible that a call to remove(element) returns true, but that the effects of the call are never ever seen by other threads (ie. contains(element) will always return true in other threads) ?

PS: Just to reassure you, I know this code is bad and I will replace the HashSet with an equivalent synchronized collection.

  • 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-18T08:13:40+00:00Added an answer on June 18, 2026 at 8:13 am

    Accessing HashSet in multiple threads can.

    • result in a change you make not being seen by another thread.
    • an Iterator to fail with a ConcurrentModifcationException.
    • a thread to go into an infinite loop inside HashSet code (rare but pretty bad when it does)

    Note: a simple replacement is to use

    Set<Type> set = Collections.newSetFromMap(new ConcurrentHashMap<Type, Boolean>());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I ran into some weird behaviour with a HashSet's iterator. In the code
I ran into a problem with some C code like this: struct SomeType {
I'm building an application which I'm also testing in Heroku. I ran into some
I was working on a project and ran into some issues which i dont
I'm trying to learn mor eabout C++ and ran into some code in a
I ran into some code I couldn't find an answer to on Google or
I've recently ran into some code that checks Page.IsCallBack but I wasn't sure how
first time posting here. I'm a beginner in jquery and i ran into some
Practicing some C++, I ran into some code from a book. The usage of
Im trying to code a popup but I ran into some problems when using

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.