Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If more than one thread can access a field should it be marked as volatile?

Reading a few threads (common concurrency problems, volatile keyword, memory model) I'm confused about concurrency issues in Java.

I have a lot of fields that are accessed by more than one thread. Should I go through them and mark them all as volatile?

When building a class I'm not aware of whether multiple threads will access it, so surely it is unsafe to let any field not be volatile, so by my understanding there's very few cases you wouldn't use it. Is this correct?

For me this is specific to version 1.5 JVMs and later, but don't feel limited to answering about my specific setup.

like image 753
Pool Avatar asked Oct 25 '09 17:10

Pool


People also ask

What will happen if multiple threads accessing the same resource?

Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.

When should volatile be used?

When To Use Volatile in C/C++ A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change: Memory-mapped peripheral registers.

Can multiple threads access the same variable?

Only one thread can read and write a shared variable at a time. When one thread is accessing a shared variable, other threads should wait until the first thread is done. This guarantees that the access to a shared variable is Atomic, and multiple threads do not interfere.

How volatile will work if we have multiple threads writing to a volatile variable?

If you write volatile variable from multiple threads without using any synchronized constructs, you are bound to get data inconsistency errors. Use volatile variables without synchronization in case of single write thread and multiple read threads for atomic operations.


2 Answers

Well, you've read those other questions and I presume you've read the answers already, so I'll just highlight some key points:

  1. are they going to change? if not, you don't need volatile
  2. if yes, then is the value of a field related to another? if yes, go to point 4
  3. how many threads will change it? if only 1, then volatile is all you need
  4. if the answer to number 2 is "no" or more than one threads is going to write to it, then volatile alone is not enough, you'll probably need to synchronize the access

Added:
If the field reference an Object, then it will have fields of its own and all those consideration also applies to these fields.

like image 75
RichN Avatar answered Sep 23 '22 15:09

RichN


If a field is accessed by multiple threads, it should be volatile or final, or accessed only with synchronized blocks. Otherwise, assigned values may not be visible to other threads.

A class has to be specifically designed for concurrent access by multiple threads. Simply marking fields volatile or final is not sufficient for thread-safety. There are consistency issues (atomicity of changes to multiple fields), concerns about inter-thread signaling (for example using wait and notify), etc.

So, it is safest to assume that an object should be visible to only a single thread unless it is documented otherwise. Making all of your objects thread-safe isn't necessary, and is costly—in terms of software speed, but more importantly, in terms of development expense.

Instead, software should be designed so that concurrent threads interact with each other as little as possible, preferably not at all. The points where they do interact need to be clearly identified so that the proper concurrency controls can be designed.

like image 28
erickson Avatar answered Sep 21 '22 15:09

erickson