Is HttpSession thread safe, are set/get Attribute thread safe operations?

Servlet 2.5 spec: Multiple servlets executing request threads may have active access to the same session object at the same time. The container must ensure that manipulation of internal data structures representing the session attributes is performed in a threadsafe manner. The Developer has the responsibility for threadsafe access to the attribute objects themselves. This … Read more

What is the difference between synchronized on lockObject and using this as the lock?

Personally I almost never lock on “this”. I usually lock on a privately held reference which I know that no other code is going to lock on. If you lock on “this” then any other code which knows about your object might choose to lock on it. While it’s unlikely to happen, it certainly could … 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

Synchronizing on String objects in Java

Without putting my brain fully into gear, from a quick scan of what you say it looks as though you need to intern() your Strings: final String firstkey = “Data-” + email; final String key = firstkey.intern(); Two Strings with the same value are otherwise not necessarily the same object. Note that this may introduce … Read more

Java Multithreading concept and join() method

You must understand , threads scheduling is controlled by thread scheduler.So, you cannot guarantee the order of execution of threads under normal circumstances. However, you can use join() to wait for a thread to complete its work. For example, in your case ob1.t.join(); This statement will not return until thread t has finished running. Try … Read more

Why can’t Java constructors be synchronized?

If you really need synchronization of the rest of the constructor versus any threads which anyhow gets a reference to your not-yet-totally-constructed object, you can use a synchronized-block: public class Test { public Test() { final Test me = this; synchronized(this) { new Thread() { @Override public void run() { // … Reference ‘me,’ the … Read more

Synchronization of non-final field

First of all, I encourage you to really try hard to deal with concurrency issues on a higher level of abstraction, i.e. solving it using classes from java.util.concurrent such as ExecutorServices, Callables, Futures etc. That being said, there’s nothing wrong with synchronizing on a non-final field per se. You just need to keep in mind … Read more