views:

176

answers:

4

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.

+3  A: 

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.

RichN
+1  A: 

The short answer is no. Threading issues require more thought and planning than this. See this for some limitations on when volatile helps for threading and when it does not. The modification of the values has to be properly synchronized, but very typically modification requires the state of more than one variable at a time. Say for example you have variable and you want to change it if it meets a criteria. The read from the array and the write to the array are different instructions, and need to be synchronized together. Volatile is not enough.

Consider also the case where the variable references a mutable object (say an array or a Collection), then interacting with that object will not be thread safe just because the reference is volatile.

Yishai
+2  A: 
erickson
+1  A: 

If you have to ask, use locks. volatile can be useful in some cases, but it's very, very difficult to get right. For example:

class Foo {
  private volatile int counter = 0;
  int Increment() {
    counter++;
    return counter;
  }
}

If two threads run Increment() at the same time, it's possible for the result to be counter = 1. This is because the computer will first retrieve counter, add one, then save it back. Volatile just forces the save and load to occur in a specific order relative to other statements.

Note that synchronized usually obviates the need for volatile - if all accesses to a given field are protected by the same monitor, volatile will never be needed.

Using volatile to make lockless algorithms is very, very difficult; stick to synchronized unless you have hard evidence that it's too slow already, and have done detailed analysis on the algorithm you plan to implement.

bdonlan
For this particular use case, see AtomicInteger, rather than using `synchronized` yourself.
erickson
Indeed. I just wanted to demonstrate the pitfalls of blindly using `synchronized` without understanding exactly what it does and does not provide
bdonlan