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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:30:23+00:00 2026-06-09T15:30:23+00:00

Have a quick synchronization question, here is what I have: a) Class1 has a

  • 0

Have a quick synchronization question, here is what I have:

a) Class1 has a concurrent hash map defined as follows:

ConcurrentMap<String, int[][]> map  = new ConcurrentHashMap<String, int[][]>();

b) Class2 has a thread, called Thread1. Thread1 creates an Id and checks if map contains it. If it does, it retrieves the value (int[][]), modifies the contents and puts it back. If it doesn’t, it creates a new int[][] and stores it. This process of check->modify/create happens frequently.

 private class Thread1 implements Runnable{

            public void run(){
                //keepRunning is volatile
                while( keepRunning ){

                  String id     = "ItemA";
                  int[][] value = map.get(id);

                  //If value is null, create an int[][] and put it back as value for Id
                  //If value is not null, modify the contents according to some logic  
                 }
             }
    }

c) Finally, I have another thread, called Thread2. This thread takes an id, checks if the map has a value for it. If it doesn’t, nothing happens. If it does, then it sums up the values in int[][] and uses the number for some calculation (no modifications here).

I am trying to figure out if my operations are atomic.
Operations in b) are fine as the creation/modification of the array and insertion into the map is confined to only one thread (Thread1).

Also, since insertion into the map establishes a happens-before action, this will ensure that c) will see the updated values in int[][].

However, I am not too sure of what would happen if Thread2 looks up the same int[][] in the map and tries to sum it up while Thread1 is modifying it.

Am I correct in thinking that Thread2 will see the older (but uncorrupted) values in int[][]. Reason being that until Thread1 has finished putting the value back into the map, the new modifications wont be visible to Thread2.

Thanks very much.

  • 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-09T15:30:25+00:00Added an answer on June 9, 2026 at 3:30 pm

    Your operations are not atomic, thread 2 will be trying to sum the values while thread 1 is modifying them.

    To avoid that you’ll need to duplicate the original modify the duplicate and put back the copy.

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

Sidebar

Related Questions

I have quick question about jQuery plugins, hope somebody out there has some advice.
I have a quick question about Java synchronization. Please assume the following code: public
I have a quick question about Java synchronization. Please assume the following code: public
I have a quick question about this PHP function here. What is the am
I have a quick question that should be simple for most people here. On
I have quick question about text parsing, for example: INPUT=a b c d e
I have quick question for you SQL gurus. I have existing tables without primary
I have a quick question about something I imagine must be pretty easy -
I have this quick sort implementation to sort the characters. int main(){ char val[]
I have a quick question that I can't seem to find online. I am

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.