Should you synchronize the run method? Why or why not?

Synchronizing the run() method of a Runnable is completely pointless unless you want to share the Runnable among multiple threads and you want to sequentialize the execution of those threads. Which is basically a contradiction in terms. There is in theory another much more complicated scenario in which you might want to synchronize the run() … Read more

What is the difference between a synchronized method and synchronized block in Java? [duplicate]

A synchronized method uses the method receiver as a lock (i.e. this for non static methods, and the enclosing class for static methods). Synchronized blocks uses the expression as a lock. So the following two methods are equivalent from locking prospective: synchronized void mymethod() { … } void mymethod() { synchronized (this) { … } … Read more

Is ConcurrentHashMap totally safe?

The get() method is thread-safe, and the other users gave you useful answers regarding this particular issue. However, although ConcurrentHashMap is a thread-safe drop-in replacement for HashMap, it is important to realize that if you are doing multiple operations you may have to change your code significantly. For example, take this code: if (!map.containsKey(key)) return … Read more