1

I am trying to find answer to my question, but not able to find it on Google or in Java docs.

in ConcurrentHashMap, suppose a thread t1 has read from segment n, and at same another thread t2 update the on the same segment n:

what is the use of concurrent read if our read by t1 became dirty because Thread t2 has updated it?

EdIted: I am expecting result of below program to be 12 but it is giving 2 or 11. that is the concern. Looks like concurrentHashMap is not safe.

package katalyst.samples;

import java.util.concurrent.ConcurrentHashMap;

public class SampleCode {
    public static void main(String[] args) throws InterruptedException {
        ThreadRun r1 = new ThreadRun();
        Thread t1 = new Thread(r1);
        t1.setName("Thread1");
        Thread t2 = new Thread(r1);
        t2.setName("Thread2");
        t2.start();
        t1.start();

        t1.join();
        t2.join();

        System.out.println(r1.getCHM().get("a"));

    }
}

class ThreadRun implements Runnable {

    ConcurrentHashMap<String, Integer> CHM = null;

    public ConcurrentHashMap<String, Integer> getCHM() {
        return CHM;
    }

    ThreadRun() {
        CHM = new ConcurrentHashMap<>();
        CHM.put("a", 1);

    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        int value = CHM.get("a");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (Thread.currentThread().getName().equals("Thread1")) {
            // int a = CHM.get("a");
            value = value + 1;
            CHM.put("a", value);
        } else {
            // int a = CHM.get("a");
            value = value + 10;
            CHM.put("a", value);
        }
    }

} 
rohit
  • 11
  • 3
  • What do you mean by "_what is the use_"? What makes you think these operations happen concurrently on the same segment? – Boris the Spider Dec 27 '17 at 18:31
  • 1
    What is the actual problem? Read will get the last ***completed*** write. – PM 77-1 Dec 27 '17 at 18:36
  • If you're talking about *modifying* values, that's not a safe way to use CHM. You need to replace values instead. – shmosel Dec 27 '17 at 18:41
  • I believe that this question has already been answered here: https://stackoverflow.com/questions/14947723/is-concurrenthashmap-totally-safe – Patrícia Espada Dec 27 '17 at 23:30
  • @BoristheSpider I have read in javaDoc that read and write can be done currently on a perticular segment of concurrentHashMap. So I am confused about the case in which one value which is read by thread1 is getting dirty when thread2 update the same. – rohit Dec 28 '17 at 11:10
  • As @PM77-1 already wrote, you will see the last completed write on reading, without problems. What do you mean by "dirty"? – egorlitvinenko Dec 28 '17 at 12:01
  • Your updates are not atomic. You need to use methods like `compute()` and `merge()` if you want this to be thread-safe. – shmosel Dec 29 '17 at 18:51

1 Answers1

0

in short, if a segment is updated and some other thread wants to read it, it will able to read, but will get last updated value(not the current/in-progress value)

Note: one thread always goes first because they cannot access the same key simultaneously. You may or may not see the object depending on when operation is performed on that key first.