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 8218083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:40:04+00:00 2026-06-07T12:40:04+00:00

i am not understanding how below code snippet can be made thread safe. class

  • 0

i am not understanding how below code snippet can be made thread safe.

class MapUser {
    Map<String,Integer> map = new ConcurrentHashMap<String,Integer>

    public void addToMap(String str, Integer val){
        if(checkMagicString(str)){
            map.put(str,val);
        }
    }

    private boolean checkMagicString(String str){
        //some logic to check Magic
        //this logic involved operation on the String parameter str i.e. subString,toCharArray etc
    }
}

Note the method addToMap is called by multiple threads concurrently. I want to make sure that thread safety is maintained. By using ConcurrentHashMap I can ensure that threads will add the values to it safely.

However I am not understanding how method checkMagicString(String str) can remain thread safe? Is the only way to do it to make it synchronized? Or should the caller method addToMap be made synchronized?
Please note that I am not accessing the map inside checkMagicString method.

  • 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-07T12:40:06+00:00Added an answer on June 7, 2026 at 12:40 pm

    Even if you do make checkMagicString atomic, it will not make the sequence

    if(checkMagicString(str)){
       map.put(str,val);
    }
    

    atomic, since a thread can be interrupted between the if check and the map.put call and thus you might end up with two threads inserting the same string. You need to lock the entire sequence to be safe.

    Edit: If the above is acceptable behavior (i.e. two threads inserting the same key and overwriting the value) and checkMagicString does not operate on shared state then your code is fine as it is.

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

Sidebar

Related Questions

My understanding is that literals are automatically interend. Let's take below code example. string
I am using the code snippet below, however it's not working quite as I
I have trouble understanding the underlying errors with the code below: class myClass {
I have some not understanding actions from gnu clisp Suppose, I have some code
I found the following code somewhere, but I am not understanding the code properly.
The Jquery code below works ok with firefox, Safari, Opera but not with IE.
I'm having problems understanding why following code leaks in one case, and not in
I have a bit of php code that I'm not understanding why it is
can anybody explain me the below code for trigger?Please note that the above code
The below code is not binding the event to an appended element (using .insertBefore()

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.