What kind of the Map interface I should use?

St.Antario

I need to make the following class thread-safe:

//Shared among all threads
public class SharedCache {

    private Map<Object, Future<Collection<Integer>>> chachedFutures;
    {
        chachedFutures = new ConcurrentHashMap<>(); //not sure about that
    }

    public Future<Collection<Integer>> ensureFuture(Object value,
                                                     FutureFactory<Collection<Integer>> ff){
        if(chachedFutures.containsKey(value))
            return chachedFutures.get(value);
        Future<Collection<Integer>> ftr = ff.create();
        chachedFutures.put(value, ftr);
        return ftr;
    }

    public Future<Collection<Integer>> remove(Object value){
        return chachedFutures.remove(value);
    }

}

After reading the article about the ConcurrentHashMap class it's still difficult for me to make a right decision.

Firstly, I tended to make the methods ensureFuture and remove just synchronized. And it would work, but from the performance standpoint it was not very good because of mutually-exclusing.

I don't know the exact (even approximately) amount of threads having access to the Cache simultaneously and the size of the Cache. Taking into account that

resizing this or any other kind of hash table is a relatively slow operation

I didn't specify the initial size of the map. Also the concurrencyLevel parameter. Is it justified to use ConcurrentHashMap here or synchronized methods would be enough?

akhil_mittal

You have following methods:

     public Future<Collection<Integer>> ensureFuture(Object value,
                                                     FutureFactory<Collection<Integer>> ff){
        if(chachedFutures.containsKey(value))
            return chachedFutures.get(value);
        Future<Collection<Integer>> ftr = ff.create();
        chachedFutures.put(value, ftr);
        return ftr;
    }

    public Future<Collection<Integer>> remove(Object value){
        return chachedFutures.remove(value);
    }

There are some points to be noticed:

  1. Suppose method ensureFuture is not synchronized in that case it is possible that one thread invokes containsKey which returns true but before next line is executed another thread may remove the entry respective to that key. This can lead to race condition as it is check-then-act scenario. Check this as well.
  2. Also you are using chachedFutures.put(value, ftr) but IMO you should use chachedFutures.putIfAbsent(value, ftr) . For this method if the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value. Using this you can also avoid contains check.

Is it justified to use ConcurrentHashMap here or synchronized methods would be enough?

It depends as CHM needs more memory compared to HashMap due to lot of bookkeeping activities etc. Another alternative is to use Collections.synchronizedMap which will provide synchronization on a regular HashMap.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What kind of the Map interface I should use?

From Dev

RAILS What kind of relation should I use?

From Dev

What kind of variable should I use?

From Dev

matplotlib - what kind of set_major_locator should I use

From Dev

What kind of Java List should I use to maintain a set of data

From Dev

What kind of outlet should I use in my UPS?

From Dev

What kind of index should I use in MYSQL with MYISAM table engine

From Dev

What kind of algorithm should I use to find similarities in a db?

From Dev

What kind of regex should I use for the following template?

From Dev

What kind of JOIN should I use to join these tables?

From Dev

What kind of NoSQL storage should we use?

From Dev

What kind of NoSQL storage should we use?

From Dev

What type should I use to define buffer size in interface?

From Dev

When should I use this kind of int variables?

From Dev

What kind of font I should use for TextView that can support all languages?

From Dev

What kind of datatypes should I use for the return value of a Web API method?

From Dev

What kind of RPM packages should I use for armv7l

From Dev

what kind of table widget module i should use for python 3x

From Dev

c# what kind of design pattern should i use when a class has image and color property

From Dev

What kind of View should I use so to get the Title and the Url of a WebView like in the image below?

From Dev

kubernetes: What kind of port should I use to expose a service behind an ingress?

From Dev

MySQL - What data type should I use to store a Map field

From Dev

What should I use for this?

From Dev

MySQL what kind of query i need to use

From Dev

MySQL what kind of query i need to use

From Dev

What kind of themes can I use on Xfce?

From Dev

What kind of SSD interface does my laptop use?

From Dev

What kind of users I should create for one mongodb cluster?

From Dev

Should I use Quartz2D to create a mind map interface?

Related Related

  1. 1

    What kind of the Map interface I should use?

  2. 2

    RAILS What kind of relation should I use?

  3. 3

    What kind of variable should I use?

  4. 4

    matplotlib - what kind of set_major_locator should I use

  5. 5

    What kind of Java List should I use to maintain a set of data

  6. 6

    What kind of outlet should I use in my UPS?

  7. 7

    What kind of index should I use in MYSQL with MYISAM table engine

  8. 8

    What kind of algorithm should I use to find similarities in a db?

  9. 9

    What kind of regex should I use for the following template?

  10. 10

    What kind of JOIN should I use to join these tables?

  11. 11

    What kind of NoSQL storage should we use?

  12. 12

    What kind of NoSQL storage should we use?

  13. 13

    What type should I use to define buffer size in interface?

  14. 14

    When should I use this kind of int variables?

  15. 15

    What kind of font I should use for TextView that can support all languages?

  16. 16

    What kind of datatypes should I use for the return value of a Web API method?

  17. 17

    What kind of RPM packages should I use for armv7l

  18. 18

    what kind of table widget module i should use for python 3x

  19. 19

    c# what kind of design pattern should i use when a class has image and color property

  20. 20

    What kind of View should I use so to get the Title and the Url of a WebView like in the image below?

  21. 21

    kubernetes: What kind of port should I use to expose a service behind an ingress?

  22. 22

    MySQL - What data type should I use to store a Map field

  23. 23

    What should I use for this?

  24. 24

    MySQL what kind of query i need to use

  25. 25

    MySQL what kind of query i need to use

  26. 26

    What kind of themes can I use on Xfce?

  27. 27

    What kind of SSD interface does my laptop use?

  28. 28

    What kind of users I should create for one mongodb cluster?

  29. 29

    Should I use Quartz2D to create a mind map interface?

HotTag

Archive