java - Lock+HasMap or ConcurrentHashMap in my case? -


i have map<string, queue<?>> , each time have put couple (key, value) need not thread-safe queue associated key , add value (if key exist). because need update existing value (queue) think best way use reentrantlock (the synchronized block or synchronized (object) in java < 1.5) , not concurrenthashmap.

is correct or can use concurrenthashmap instead of hashmap + lock? think concurrenthashmap more efficient in operation don't know if right solution here.

    private static reentrantlock lock_vqueue = new reentrantlock();     private static hashmap<string,queue<documentobjectholder>> cachedata = new hashmap<string,queue<documentobjectholder>>();         /**          * insert element in tail           * sort elements priority           * @param obj documentobjectholder object          */         public static void add(string key, documentobjectholder obj){             reentrantlock lock = lock_vqueue; //performance side effect             try{                 queue<documentobjectholder>priorityprocessingvirtualqueue;                 lock.lock();                     if (!cachedata.containskey(key)){                         priorityprocessingvirtualqueue = new priorityqueue<documentobjectholder>(1, new prioritycomparator());                         cachedata.put(key, priorityprocessingvirtualqueue);                     }                     priorityprocessingvirtualqueue = cachedata.get(key);                     priorityprocessingvirtualqueue.add(obj);                     cachedata.put(key, priorityprocessingvirtualqueue);             }finally {                 lock.unlock();             }         }          /**          *           * @return documentobjectholder instance head of list (fifo)          */         public documentobjectholder get(string key){             reentrantlock lock = lock_vqueue; //performance side effect             queue<documentobjectholder>priorityprocessingvirtualqueue;             try {                 lock.lock();                   if (cachedata.containskey(key)){                      priorityprocessingvirtualqueue = cachedata.get(key);                      return priorityprocessingvirtualqueue.poll();                  }                  return null;             }finally{                 lock.unlock();             }         } } 

is code right or more performant?

so have 2 separate atomic instructions need synchronized with.

  1. putting new queue map
  2. putting object queue

here suggestions.

  1. continue use concurrenthashmap. still need put map , may safely chm.

  2. use blockingqueue. in case can use priorityblockingqueue.

  3. if cannot (2) synchronize on queue map.

so 1 & 3 like:

    public static void add(string key, documentobjectholder obj){         queue<documentobjectholder> priorityprocessingvirtualqueue= cachedata.get(key);         if(priorityprocessingvirtualqueue== null){              queue<documentobjectholder> temp = new priorityqueue<documentobjectholder>(1, new prioritycomparator());              queue = cachedata.putifabsent(key, temp);              if(priorityprocessingvirtualqueue== null){                    priorityprocessingvirtualqueue= temp;              }          }         synchronized(priorityprocessingvirtualqueue){             priorityprocessingvirtualqueue.add(obj);         }     } 

the difference needed 1 & 2 absence of synchronized.

the reason know thread-safe because if 2 or more threads enter if(queue == null) 1 succeed in putifabsent. threads lose assign queue equal queue put in. if thread wins (queue == null true) assign queue 1 we created (we being winning thread).


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -