1

I have a HashMap which is static and three threads which try to access HashMap simultaneously from their corresponding class`s.

each thread task is get list value of a specified key, process some operations on the list(modify the list). and put the processed list in HashMap.

I want to make other threads trying to access the HashMap wait until current thread finishes the processing and modifying the HashMap.

in some situation, the flow is like this,

thread A is retrieved HashMap, while Thread A is processing on the list of HashMap, other Thread B retrieves the HashMap and starts its processing.

Actual behaviour has to be like: Thread A -> retrieves HashMap -> process -> put value in HashMap. Thread B -> retrieves HashMap -> process -> put value in HashMap. Thread C -> retrieves HashMap -> process -> put value in HashMap.

logic :

  1. apply lock on HashMap
  2. retrieve.
  3. process.
  4. put into HashMap.
  5. release lock.

help me in converting the logic to code, or any suggestions are accepted with smile.

3
  • Why not just use ConcurrentHashMap?
    – John Vint
    Commented Mar 30, 2017 at 14:00
  • ConcurrentHashMap takes care of locking the hashMap only until that particular statement runs. but in my class I want the lock to be held until it puts the processed data into hashMap. Commented Mar 30, 2017 at 14:16
  • If "that particular statement" instead was .computeIfPresent() you'd be all set.
    – Oskar Lund
    Commented May 27, 2020 at 22:06

3 Answers 3

4

You can really make use the ReentrantReadWriteLock. Here is the link for that.

Javadoc for ReadWriteReentrant lock

I would implement the feature as something like this..........

public class Test {

        private Map<Object, Object> map = new HashMap<>();
        private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();

        public void process() {
            methodThatModifiesMap();
            methodThatJustReadsmap();
        }

        private void methodThatModifiesMap() {
            //if the code involves modifying the structure of the map like 'put(), remove()' i will acquire the write reentrantReadWriteLock
            reentrantReadWriteLock.writeLock().lock();
            try {
                //DO your thing and put() or remove from map
            }
            finally {
                //Dont forget to unlock
                reentrantReadWriteLock.writeLock().unlock();
            }
        }

        private void methodThatJustReadsmap() {
            // if all you are doing is reading ie 'get()'
            reentrantReadWriteLock.readLock().lock(); // this does not block other reads from other threads as long as there is no writes during this thread's read
            try {

            } finally {
                reentrantReadWriteLock.readLock().unlock();
            }
        }

    }

Not only your map is thread-safe, the throughput is better too.

1

You can use ConcurrentHashMap instead of HashMap. The ConcurrentHashMap gives better performance and reduces overhead of locking the whole HashMap while other thread is accessing it. You can find more details on this page as well - http://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/

4
  • Worth noting ConcurrentHashMap is not actually a drop-in replacement for HashMap because the former does not allow null keys or values while the latter does.
    – Michael
    Commented Mar 30, 2017 at 14:12
  • ConcurrentHashMap takes care of locking the hashMap only until that particular statement runs. but in my class I want the lock to be held until it puts the processed data into hashMap. Commented Mar 30, 2017 at 14:15
  • Make the method synchronized? Commented Mar 30, 2017 at 14:23
  • what @LuudvanKeulen meant is mark the method where you have this implentation of processing and putting the data in the hashmap as synchronized. e.g.` public void synchronized addToMap(Object o){//code to add the data to hashmap}`. However please note that this may have performance impact
    – vlaxmi
    Commented Mar 30, 2017 at 14:52
0

You can either use ConcurrentHashMap as suggested above or use class level locks.What I mean by it is by using synchronized keyword on static method.eg

public class SynchronizedExample extends Thread {
    static HashMap map = new HashMap();
    public synchronized static void execute() {
        //Modify and read HashMap
    }

    public void run() {
            execute();
    }
}

Also as others mentioned it will incur performance bottlenecks if you use synchronized methods, depends on how atomic functions you make. Also you can check class level locks vs object level locks(Although its almost same, but do check that.)

1
  • Modification logic varies from where i'm calling from, i.e., at one instance the modification logic is diffrnt, to other instances. Commented May 6, 2017 at 13:02

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.