2

I need a container that contains [key, Value] pair.

Here, key = Integer, Value = User Defined class object.

Mutiple threads are trying to add [key, Value] pair in above container.

If key already present in the container, I want to update the value by checking some condition.

At the end I want container in sorted order, according to Key.

My efforts -

I used this synchronizedSortedMap and Sorted Map for above task.

SortedMap<Integer, USER_DEFINED_OBJECT> m = Collections.synchronizedSortedMap(new TreeMap<Integer, USER_DEFINED_OBJECT>());

This helps me to add pairs concurrently on above container.

And, yes If key already present, then I check some condition, then proceed.

Is my approach always thread safe ? If not, please correct me.

Updated

  1. USER_DEFINED_OBJECT has some field index.
  2. At the time of adding, I am checking if key is already present, then compare current USER_DEFINED_OBJECT with already present USER_DEFINED_OBJECT on the basis of above mentioned(in point 1) filed "index". If currect "index" is greater than update.
Loha
  • 50
  • 6
  • http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html – DmitryKanunnikoff Jul 01 '14 at 05:59
  • As long as you synchronize the whole "adding, updating" process and not only the adding, this should be thread safe. – Smutje Jul 01 '14 at 05:59
  • `ConcurrentHashMap` is solution, and then sort it as your desired order. Take a look [this](http://stackoverflow.com/questions/5942474/sort-concurrent-map-entries-by-value) – Wundwin Born Jul 01 '14 at 06:08
  • To tell whether your code is thread safe, please supply the rest of it. Specifically the checking of the condition. It is possible that you have race conditions. – Chris K Jul 01 '14 at 07:46
  • @ChrisK Please check updated part. – Loha Jul 01 '14 at 07:57

3 Answers3

3

Use ConcurrentHashMap from java.util package, read the API ConcurrentHashMap

Udit Mishra
  • 150
  • 11
3

java.util.concurrent.ConcurrentSkipListMap

A scalable concurrent ConcurrentNavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. This class implements a concurrent variant of SkipLists providing expected average log(n) time cost for the containsKey, get, put and remove operations and their variants. Insertion, removal, update, and access operations safely execute concurrently by multiple threads. Iterators are weakly consistent, returning elements reflecting the state of the map at some point at or since the creation of the iterator. They do not throw ConcurrentModificationException, and may proceed concurrently with other operations. Ascending key ordered views and their iterators are faster than descending ones.

Sergey Morozov
  • 4,528
  • 3
  • 25
  • 39
  • thanks for your answer. But I am confused on one point, yes we can add multiple entries concurrently. But adding any entry I need to check, whether that entry is already present or not. I think there may be a situation comes whn one thread checking condition, and it found pair is not present in container, go and put. but at the same time some other thread put som value for that key. Is this situation ever happen ? – Loha Jul 01 '14 at 06:18
  • 1
    It dependes on logic of your application. If you need you should to check. :-). Describet situation can happens. This class have method putIfAbsent - add pair only if it not present. – Sergey Morozov Jul 01 '14 at 06:36
1

The concurrent collections let you call methods like put, remove etc. in kind of transaction. Therefore it's thread safe.

From what I understood your scenario for adding new [key, value] pair is as follows:

  1. Check whether the mapping already exists
  2. If not, just add it
  3. If yes, update the existing value in the mapping based on some check

I doubt there is an implementation in place which does this for you in thread-safe way. In the case I understood your use-case correctly you will need to add some manual synchronization on your own to make the update steps transactional.

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
  • Yes exactly, I make check condition and update container as atomic, by putting in synchronized block on container object. – Loha Jul 01 '14 at 08:37