1

I have a static concurrentHashMap object which is been updated in background. While it is getting updated, I want to access the values from it in another thread. I am using concurrentHashMap which I understand from the documentation and thinks that it would suit this scenrio

Here is what I am doing

for (Map.Entry<String, ArrayList<property>> entry : alldata.entrySet())
        {
            udata.put(entry.getKey(), new ArrayList<property>(entry.getValue()));
        }

in above code I am updating udata on getting data from server but in background.

While in another thread I am accessing some info out of it ..

  for (String s: sTypes) 
        {
                    if(jprocess.udata != null)
                        {
                            if (jprocess.udata.get(s) != null)
                                {
                                    if (jprocess.udata.get(s).size() > 0) {
                                        if (xcor < jprocess.udata.get(s).size())
                                            if (xcor != -1) {
                                                allData.add(jprocess.udata.get(s).get(xcor));
                                            }
                                    }
                                }
                        }
                }

but when I try to access any indexfrom it I can not access anything I hope it is clear what I want..

I already tried ConcurrentHashMap which should work for this situation but may be I did not understand it well..

7
  • which index do you mean? Feb 14, 2015 at 16:05
  • any index from hashmap respective to key .... Feb 14, 2015 at 16:06
  • first, call it value in order to follow common language, second, are you sure you start accessing data after the map becomes populated? Feb 14, 2015 at 16:09
  • that is my point, the map is getting populated contineously in background....so I want to access existing value from it while it is getting appended ...with new data .. Feb 14, 2015 at 16:13
  • 1
    I believe you need to take a look at CountDownLatch() and other classes from java.util.concurrency package. Feb 14, 2015 at 16:14

1 Answer 1

1
+50

If you are using ConcurrentHashMap, then there is not point it will not work. I strongly doubt that you are trying to access wrong index or you do not have synchronization between inserting and accessing the particular index.

For that just check the size of udata and value of xcor in your code and in each if condition.

In addition, read this carefully and for sure you will be convinced that ConcurrentHashMap is right solution for your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.